【问题标题】:reselecting all checkboxes using jquery causing weird action使用 jquery 重新选择所有复选框导致奇怪的动作
【发布时间】:2014-05-12 19:59:02
【问题描述】:

我正在使用 jQuery 来处理选择表单的所有复选框。这是我的代码:

    $(function () {
        $('#selectall').on('click', function() {
            $('.selectedId').attr('checked', $(this).is(":checked"));       
        });
    });

    <input type="checkbox" id="selectall">Select all</input>
    <input type="checkbox" name="industry1" id="industry1" value="1" class="selectedId">
    <input type="checkbox" name="industry2" id="industry2" value="1" class="selectedId">

当我检查selectall id checkbox 时,它会选择所有具有selectedId 类的复选框。当我再次单击它时,它会删除对所有的检查。

我的问题是,在初始全选/取消全选之后,它不再起作用,因为我无法全选。

知道我应该在我的 javascript 中做什么来解决这个问题吗?

谢谢!

【问题讨论】:

标签: javascript jquery html checkbox onclick


【解决方案1】:

使用复选框属性而不是它们的属性

$(function () {
    $('#selectall').on('click', function() {
        $('.selectedId').prop('checked', this.checked);       
    });
});

http://jsfiddle.net/VmnE2/

【讨论】:

    猜你喜欢
    • 2013-01-24
    • 2014-06-29
    • 1970-01-01
    • 2015-05-09
    • 2011-01-14
    • 2012-05-08
    • 2013-01-29
    • 2018-09-15
    • 2020-01-16
    相关资源
    最近更新 更多