微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jquery选择表中的所有复选框

我有一个脚本,应该检查表中的所有复选框.它首次检查它们,之后它会取消选中它们.但是,当我尝试重新检查它们什么也没有发生.

jquery

$('#selectAll').click(function(e){
    var table= $(e.target).closest('table');
    $('td input:checkBox',table).attr('checked',e.target.checked);
});

HTML:

<table>
    <tr>
        <th>
            <input type="checkBox" id="selectAll" />
        </th>
        <th>
            hi!
        </th>
    </tr>
    <tr>
        <td>
            <input type="checkBox" id="1"/>
        </td>
        <td>
            hi!
        </td>
    </tr>
    <tr>
        <td>
            <input type="checkBox" id="2"/>
        </td>
        <td>
            hi!
        </td>
    </tr>
</table>

这是一个行为的小提琴:

http://jsfiddle.net/EEJrU/

为什么点击一次后不起作用?

解决方法

您需要使用 .prop()而不是 .attr()
$('#selectAll').click(function(e){
    var table= $(e.target).closest('table');
    $('td input:checkBox',table).prop('checked',this.checked);
});

演示:Fiddle

Attributes vs. Properties

原文地址:https://www.jb51.cc/jquery/179936.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐