简体   繁体   English

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

[英]Why is my jQuery if/else if statement not working?

The syntax appears to be correct to me, but only the "if" statement functions.语法对我来说似乎是正确的,但只有“if”语句起作用。 I have even tried with just a simple if/else statement and only the "if" statement functions that way as well.我什至只尝试了一个简单的 if/else 语句,并且只有“if”语句也以这种方式起作用。 What am I doing wrong?我究竟做错了什么? I have seen similar questions on SO as well, but they did not answer my question because when I tried to use their corrections in my case, it still didn't work.我在 SO 上也看到过类似的问题,但他们没有回答我的问题,因为当我尝试在我的案例中使用他们的更正时,它仍然不起作用。

HTML HTML

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

JS 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>

your condition is structured correctly, except for comparison with the current value true or false您的条件结构正确,除了与当前值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>

Youre not checking the checked , but setting it:您没有检查checked ,而是设置它:

// This part sets the prop checked to false
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked', false)) {

// This would check if it is false
if ($(this).closest('td').prev().find('input[type=checkbox]').prop('checked') === false) {

Also, you can just use } else { ;此外,您可以使用} else { ; no need to check if its true , because thats what the else will do if the if is false .无需检查它是否为true ,因为如果iffalse ,那么else会做什么。

From the code it looks like you just want to toggle the checkbox when the label click me is clicked.从代码中,您似乎只想在单击 label click me 时切换复选框。 No need for Javascript or JQuery at all.根本不需要 Javascript 或 JQuery。 You can just wrap the input in a label tag and put your clickable text after the checkbox input and it will work when the label is clicked.您可以将输入包装在 label 标记中,并将可点击的文本放在复选框输入之后,当点击 label 时它将起作用。

If you need a function to run when the checkbox is toggled it will be easier doing it from the change event of the checkbox than using 2 separate objects as toggles.如果您需要在切换复选框时运行 function ,那么从复选框的change事件中执行此操作会比使用 2 个单独的对象作为切换更容易。

 $(".checkbox").change(function() { if(this.checked) { console.log('fires when checked == true') } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td> <label> <input type="checkbox" class="checkbox"/> click me </label> </td> </tr> </table>

First of all, you are overthinking it.首先,你想多了。 I do not see the need of having to trigger the action for an input field from another TD with a link.我认为不需要从另一个带有链接的 TD 触发输入字段的操作。 That is not semantic and it just adds to the size of your document unnecessarily.这不是语义化的,它只会不必要地增加文档的大小。 I would handle it as shown below.我会如下所示处理它。

 $(function () { $(".checkbox").change(function() { if ($(this).is(':checked')) { // Take initial action if checked $(this).parents("td").find(".content").attr("contentEditable", true).focus(); } else { // Take reverse action if unchecked $(this).parents("td").find(".content").attr("contentEditable", false).blur(); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td> <div class="content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce faucibus pulvinar nulla. Curabitur ultricies eleifend ante, porttitor elementum dui bibendum vel. Cras convallis nibh quis ipsum pulvinar, eget vulputate enim semper. Praesent rutrum nisl enim, ac suscipit urna posuere at. In id purus enim. Aenean ut tempus diam, sit amet pulvinar diam. Quisque purus lorem, condimentum non nibh sed, elementum faucibus tellus. Nulla pulvinar ligula eget ipsum finibus feugiat. Praesent placerat orci id metus facilisis, id euismod augue euismod. Vestibulum imperdiet neque vel eros scelerisque pharetra. Aliquam erat volutpat.</div> <br> <label> <input type="checkbox" class="checkbox"/> Edit </label> </td> </tr> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM