【问题标题】:Select/Deselect checkbox does not work after selectAll checkbox clicked选中所有复选框后,选择/取消选择复选框不起作用
【发布时间】:2015-11-27 15:32:21
【问题描述】:

我有一个 jquery 数据表。如您所见,其中一列包含复选框 here 。现在,当我单击行上的复选框时,它们可以正常工作。我的意思是它会打印出一个单选类复选框被选中一个单选类复选框未被选中,因为我在事件处理程序中添加了这些。但是,如果我单击全选一次,我的各个复选框将不再运行(它不会打印出这些消息)。全选复选框看起来工作正常。 (选择所有单个框)。这是我的全选和单选复选框相关代码:

SelectAll 处理程序:

$('input[type="checkbox"][id="selectAll"]').click(function () {
    if ($(this).prop("checked") == true) {
        converter.checkAll(true);

    } else if ($(this).prop("checked") == false) {
        converter.checkAll(false);
    }
});

checkAll 功能

Converter.prototype.checkAll = function (checktoggle) {
    var data = (this.resultTable).fnGetData();
    for (var i in data) {
        var row = $('<div>' + data[i][3] + '</div> ');

        row.children('input').attr('checked', (checktoggle) ? 'checked' : false);
        row.children('input').attr('id', 'singleSelect');
        console.log(row.html());

        (this.resultTable).fnUpdate(row.html(), parseInt(i, 10), 3, false, false);
    }
    (this.resultTable).fnDraw();
}

单选处理程序

$('input[type="checkbox"][id="singleSelect"]').click(function () {

    alert("asdasdasdasd");
    if ($(this).prop("checked") == true) {
        alert('one of the single select class checkbox is checked');
    } else if ($(this).prop("checked") == false) {

        alert('one of the single select class checkbox is unchecked');
    }
});

【问题讨论】:

  • 你能提供html或更好的小提琴
  • converter 是什么?
  • @guest271314 转换器在 Converter.prototype 函数之一中初始化为“this”关键字
  • @JSantosh 我的项目太大了,由几个依赖项目组成,所以我不知道如何为项目提供一个小提琴
  • @zwlayer "converter is initialized to 'this' keyword inside one of Converter.prototype function" thisconverter.checkAll(true); , var data = (this.resultTable).fnGetData(); 上是否相同?

标签: javascript jquery checkbox


【解决方案1】:

使用此模式

SelectAll 处理程序:

// note change event
$('input[type="checkbox"][id="selectAll"]').change(function () {
        converter.checkAll($(this).prop("checked"));
});

checkAll 功能

Converter.prototype.checkAll = function (checktoggle) {
    var data = (this.resultTable).fnGetData();
    for (var i in data) {
        var row = $('<div>' + data[i][3] + '</div> ');

        row.children('input').prop('checked', checktoggle) ;
        row.children('input').attr('id', 'singleSelect');
        console.log(row.html());

        (this.resultTable).fnUpdate(row.html(), parseInt(i, 10), 3, false, false);
    }
    (this.resultTable).fnDraw();
}

单选处理程序

$('input[type="checkbox"][id="singleSelect"]').change(function () {

    alert("asdasdasdasd");
    if ($(this).prop("checked") == true) {
        alert('one of the single select class checkbox is checked');
    } else if ($(this).prop("checked") == false) {

        alert('one of the single select class checkbox is unchecked');
    }
});

【讨论】:

  • 抱歉,如果有错字,我现在在移动端
  • 感谢您的回复,我用您的代码替换了我的代码,但它不起作用。现在,全选甚至不选择单个复选框
  • 所以分享你的html,因为有些东西我用水晶球看不到
【解决方案2】:

问题似乎是

$('input[type="checkbox"][id="selectAll"]').click(function () {

input 上调用,带有选择器 [id="selectAll"]js at post 不会取消选中所有复选框,而是创建新的 input 元素,其中 checked 属性设置为参数 checktoggle


那么我应该怎么做才能解决这个问题呢?

如果没有 html 包含在 Question 中,很难确认。尝试使用 .each() ,将 this.checked 设置为 truefalse 在回调中

$('input[type="checkbox"][id="selectAll"]').click(function (e) {
  $("input[type=checkbox]").each(function() {
    if ($(e.target).prop("checked") == true) {
        this.checked = true;
        converter.checkAll(true);

    } else  {
        this.checked = false;
        converter.checkAll(false);
    }
  })
});

【讨论】:

  • 无论如何都可以在选中/未选中之间更改它的状态,而不是每次都创建新的复选框?
  • @zwlayer “有没有办法在选中/未选中之间改变它的状态,而不是每次都创建新的复选框?” 不完全确定预期的结果是什么?可以创建 stacksn-ps , jsfiddle jsfiddle.net 来演示吗?查看更新后的帖子,stackoverflow.com/help/mcve
【解决方案3】:

我们可以通过使用子行的复选框名称来检查/取消选中 jQuery 数据表。

<script type="text/javascript">
    $(function () {
        $("#chkAll").click(function () {
            $("input[name='employees']").attr("checked", this.checked);
        });
        $('#example').DataTable({
        });
    });
</script>

<table id="example" class="display" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th align="left"><input type="checkbox" id="chkAll" /></th>
                <th>Name</th>
                <th>Position</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" name="employees" /></td>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>$320,800</td>
            </tr>
            <tr>
                <td><input type="checkbox" name="employees" /></td>
                <td>Garrett Winters</td>
                <td>Accountant</td>

                <td>$170,750</td>
            </tr>
          </tbody>
    </table>

它可以正常工作以获取更多详细信息http://www.infinetsoft.com/Post/How-to-check-all-rows-in-jQuery-datatables-grid/1255

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 2014-05-23
    • 2021-03-22
    相关资源
    最近更新 更多