jquery - 检查数据表中的所有复选框,包括隐藏行

标签 jquery datatable webforms

我正在尝试创建一个函数来检查数据表中的所有复选框,包括隐藏行。 以下是“复选框”列的 html 代码:

<div class="usersTable" id="userTable">
    <table cellpadding="0" cellspacing="0" id="customersList" >
        <thead>
            <tr>
                <th><input type="checkbox" name="selectall" id="selectall" class="selectall"/></th>
                <th width="200">val1</th>
                <th width="80px">val2</th>
                <th width="70px">val3</th>
                <th width="450">val4</th>
                <th width="60px">val5</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

提交按钮:

<input type='button' value='select all' id='selectallboxes' name='selectallboxes' />

并且 JQuery 代码不起作用:

$(function () {         
    otable = $('#customersList').dataTable({
        "bJQueryUI": true,
        "sPaginationType": "full_numbers",
        "aLengthMenu" : [ [10,20,50,100,1000], [10,20,50,100,1000] ],
        "iDisplayLength": 100,
        "bProcessing": true,
        "bServerSide": true,
        "aaSorting":[],         
        "iDisplayStart": 0,
        "sAjaxSource": "filename",
        ....

$("#selectallboxes").click ( function () {
        alert(dt.fnGetNodes().length + ' is total number')
        var selected = new Array();
        $('input', dt.fnGetNodes()).each( function() {
                $(this).attr('checked','checked');
                selected.push($(this).val());                       
        } );
         // convert to a string
        var mystring = selected.length;
        alert(mystring);
})

最佳答案

尝试:

$("#selectallboxes").click(function () {
    var selected = new Array();
    $(otable.fnGetNodes()).find(':checkbox').each(function () {
        $this = $(this);
        $this.attr('checked', 'checked');
        selected.push($this.val());
    });
    // convert to a string
    var mystring = selected.join();
    alert(mystring);
});

.length 给出数组的长度。我使用 join() 将数组连接到字符串中。 DataTable 的 .fnGetNodes() 为您提供表中的所有行,包括隐藏行。

关于jquery - 检查数据表中的所有复选框,包括隐藏行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15458783/

相关文章:

jQuery 创建空的自闭标签

c# - 将格式应用于 DataTable 中的列

c# - 如何在这个简单的 asp.net webforms 应用程序中拥有和管理 session 超时?

c# - 在 ASP.NET 页面上填充表格

javascript - 将 jSignature 添加到 Bootstrap 表单

jquery - Slick.js 显示当前幻灯片和幻灯片总数

javascript - 旋转一束绝对定位的 div - <edited> 而不将它们放入容器中

java - 如何在 JSF 中的数据表顶部添加新行?

c# - 除了返回一行之外,DataTable.NewRow 还执行其他操作吗?

javascript - 由于文档准备好且 DOM 较大,jquery 点击触发速度非常慢