选中和取消选中复选框并读取其状态

发布于 2024-10-05 06:25:45 字数 515 浏览 0 评论 0原文

如何读取复选框的状态并将其状态设置为可见?我正在尝试这样的事情,但它总是返回 true:

 $('.show-check').live('click', function(e) {
    e.preventDefault();

    var checked = ($(this).is(':checked'));
    if(checked == true){
       $(this).attr('checked', true); 
    }
    else{
        $(this).attr('checked', false);
    }
 });


    <li>
        <img src="" class="show-image"/>
        <span class="show-title"></span>
        <input type="checkbox" class="show-check" />
    </li>

How can I read the state of my checkbox and set it's state to be visible ? I'm trying something like this, but it always returns true:

 $('.show-check').live('click', function(e) {
    e.preventDefault();

    var checked = ($(this).is(':checked'));
    if(checked == true){
       $(this).attr('checked', true); 
    }
    else{
        $(this).attr('checked', false);
    }
 });


    <li>
        <img src="" class="show-image"/>
        <span class="show-title"></span>
        <input type="checkbox" class="show-check" />
    </li>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动听の歌 2024-10-12 06:25:45

它是这样工作的:

 $('.show-check').live('click', function() {


var checked = ($(this).is(':checked'));

if(checked){
   $(this).attr('checked', true);
}
else{
    $(this).attr('checked', false);
}

});

我认为您无法选中该复选框,因为默认操作被阻止,即选中/取消选中该框,

这是我的想法:

  1. 该复选框最初未选中;

  2. 当您单击复选框时,会触发 click 事件,并且 varchecked 设置为 true;

  3. 因为它设置为 true,所以 if(checked) 块将始终被执行;

  4. 但您可能想知道为什么 UI 上未选中该复选框。我认为这是因为你设置了e.preventDefault(),它告诉复选框不受点击事件的默认行为的影响,即检查它;

  5. 下次您再次检查时,会发生同样的事情。

It works this way:

 $('.show-check').live('click', function() {


var checked = ($(this).is(':checked'));

if(checked){
   $(this).attr('checked', true);
}
else{
    $(this).attr('checked', false);
}

});

I think you couldn't check the checkbox because the default action is prevented, which is to check/uncheck the box

Here's my thought:

  1. the checkbox is initially unchecked;

  2. when you click the checkbox, the click event is fired, and the var checked is set to true;

  3. since it's set to true, the if(checked) block will always be executed;

  4. but you may wonder why the checkbox is not checked on the UI. I think it's because you set e.preventDefault() which tells the checkbox not to be affected by the default behaviour of click event, which is to check it;

  5. the next time you check it again, same thing will happen.

旧时模样 2024-10-12 06:25:45

如果您使用 .change() 事件而不是 .clecked() 事件,也许这会更清楚。

您是说,当单击复选框时,如果checked==true,则设置checked=true,因此如果您从选中状态开始,则 false 部分将永远不会被执行。如果您一开始将其设置为 false,情况也是如此。

Maybe this would be clearer if you used the .change() event instead of the .clecked() event.

You are saying when the check box is clicked, if checked==true than set checked = true, so the false part will never be executed if you start with it checked. The same is true if you start with it set to false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文