为什么我的jQuery if / else if语句不起作用?

grahamfk45c

该语法对我来说似乎是正确的,但是只有“ if”语句起作用。我什至尝试仅使用一个简单的if / else语句,并且只有“ if”语句也是如此。我究竟做错了什么?我也曾在SO上看到过类似的问题,但是他们没有回答我的问题,因为当我尝试在我的案例中使用他们的更正时,它仍然无法正常工作。

的HTML

<table>
  <tr>
    <td><input type="checkbox" value="test" /></td>
    <td><a class="clickme" href="#">edit</a></td>
  </tr>
</table>

JS

$('.clickme').click(function () {
    if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false)) {
        $(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true);
        console.log('Test2');
    } else if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true)) {
        $(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false);
        console.log('Test');
    }
});

$('.clickme').click(function () {
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false)) {
    $(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true);
    console.log('Test2');} else if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true)) {
    $(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false);
    console.log('Test');
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td><input type="checkbox" value="test" /></td>
    <td><a class="clickme" href="#">edit</a></td>
  </tr>
</table>

谢尔盖·库兹涅佐夫

你的条件是正确的结构,除了与当前值进行比较truefalse

$('.clickme').click(function () {
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked') == false) {
    $(this).closest('td').prev().find('input[type=checkbox]').prop('checked', true);
    console.log('Test2');} else 
    if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked') == true) {
    $(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false);
    console.log('Test');
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td><input type="checkbox" value="test" /></td>
    <td><a class="clickme" href="#">edit</a></td>
  </tr>
</table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么else语句在php的foreach循环内不起作用?

如果语句不起作用,如何修复jQuery Multiple else

为什么我的if else语句(即?:)不起作用?

为什么此代码不起作用?Javascript if语句,else if语句

jQuery else if语句似乎不起作用

为什么JavaScript if / else语句不起作用

jQuery图像交换if-else语句不起作用

为什么我的if,elseif,else语句不起作用?

为什么我的JavaScript如果else语句不起作用?

为什么我的if ... else语句不起作用,我的错误在哪里以及如何解决?

jQuery if / else语句不起作用

Python-我的else语句不起作用

为什么我的if else语句不起作用?jQuery的

jQuery在If / Else语句中隐藏/显示不起作用

为什么我的if-else-if语句在我的数组列表中不起作用?

为什么我的else陈述不起作用?

为什么我的Javascript setInterval函数中的else语句不起作用?

为什么 if/else 语句在 Object.assign() 和 .map 中不起作用?

为什么这个 if else 语句不起作用 - 水壶 javascript

jQuery addClass 和 removeClass 在 if else 语句中不起作用

为什么 if/else 语句在我的函数中不起作用?

Jquery Onchange If else 语句不起作用

为什么我的“else”语句在 python 中不起作用?

我认为我的 if else 语句不起作用

如果 else 语句不起作用,我的真假

else if 语句在我放入 while 循环时不起作用

为什么我的 else if 语句在observeEvent 中不起作用?

(JavaScript) 为什么我的 if/else 代码不起作用?

我的 If/Else 语句不起作用?是我的语法吗?