选择所有复选框只有两次 [英] select all checkbox only works twice

查看:143
本文介绍了选择所有复选框只有两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从stackoverflow这个代码看起来像一个很好的选择所有复选框解决方案,任何想法为什么它失败后第二次点击?



http://jsfiddle.net/R9zjk/2 /

 < table> 
< tr>
< td>
< input type ='checkbox'value ='0'class =''>
< / td>
< td>
< input type ='checkbox'value ='0'class =''>
< / td>
< td>
< input type ='checkbox'value ='0'class =''>
< / td>
< td width =100align =right>
select all< input type ='checkbox'value ='0'class ='selectall2'>
< / td>
< / tr>
< / table>

$(document).ready(function(){


$(document).on(click,.selectall2,function


$(this).closest('tr')。find('input [type = checkbox]')。attr('checked',this.checked);


});



});


解决方案

use .prop c>


$ b

如果使用jQuery 1.6,代码 if($(elem).attr(checked))将检索实际内容属性检查和取消选中。它仅用于存储checked属性的默认值或初始值。为了保持向后兼容性,jQuery 1.6.1+中的 .attr()方法将检索并更新您的属性,因此无需将布尔属性的代码更改为 .prop()。然而,检索检查值的首选方法是使用上面列出的选项之一。要查看最新jQuery中的效果,请选中/取消选中以下示例中的复选框。



请参阅 .prop()



demo - http://jsfiddle.net/f9QYx/


I got this code from stackoverflow which looks like a pretty good "select all" checkbox solution, any ideas why it fails after 2nd click?

http://jsfiddle.net/R9zjk/2/

<table>
    <tr>
        <td>
            <input type='checkbox' value='0' class=''>
        </td>
        <td>
            <input type='checkbox' value='0' class=''>
        </td>
        <td>
            <input type='checkbox' value='0' class=''>
        </td>
        <td width="100" align="right">
            select all <input type='checkbox' value='0' class='selectall2'>
        </td>
    </tr>
</table>

$(document).ready(function () {


    $(document).on("click", ".selectall2", function () {


        $(this).closest('tr').find('input[type=checkbox]').attr('checked', this.checked);


    });



});

解决方案

use .prop() instead of .attr() above jQuery 1.6

If using jQuery 1.6, the code if ( $(elem).attr("checked") ) will retrieve the actual content attribute, which does not change as the checkbox is checked and unchecked. It is meant only to store the default or initial value of the checked property. To maintain backwards compatability, the .attr() method in jQuery 1.6.1+ will retrieve and update the property for you so no code for boolean attributes is required to be changed to .prop(). Nevertheless, the preferred way to retrieve a checked value is with one of the options listed above. To see how this works in the latest jQuery, check/uncheck the checkbox in the example below.

See .prop()

demo - http://jsfiddle.net/f9QYx/

这篇关于选择所有复选框只有两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆