2

我为此创建了自己的功能-

function countchecked(){
var i=0;
$('tbody[role="alert"] input:checkbox.multi-select').each(function () {
   var sThisVal = (this.checked ? $(this).attr("uid") : "0");
   if(sThisVal!="0")
   {
    i++;
   }
    });
   return i;
}

此功能适用于静态复选框,但当我添加动态复选框时,它无法找到它们。

这是动态添加所有复选框的代码。这是每个复选框所在的tr-

<table class="dynamicTable table table-striped table-bordered table-condensed dataTable" id="DataTables_Table_0" aria-describedby="DataTables_Table_0_info">

    <!-- Table heading -->

    <thead>
        <tr role="row"><th class="sorting_asc" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-sort="ascending" aria-label="Name: activate to sort column descending" style="width: 133px;">Name</th><th class="sorting" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Email: activate to sort column ascending" style="width: 225px;">Email</th><th class="center sorting" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Registration Date: activate to sort column ascending" style="width: 155px;">Registration Date</th><th class="center sorting" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Last Login: activate to sort column ascending" style="width: 154px;">Last Login</th><th class="center sorting" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Active: activate to sort column ascending" style="width: 59px;">Active</th><th class="sorting" role="columnheader" tabindex="0" aria-controls="DataTables_Table_0" rowspan="1" colspan="1" aria-label="Tools : activate to sort column ascending" style="width: 75px;">Tools <a class="point" id="del" uid="'.$item['id'].'"><img src="/lifelist/images/del_icon.png"></a></th></tr>
    </thead>
    <!-- // Table heading END -->

    <!-- Table body -->

    <!-- // Table body END -->

<tbody role="alert" aria-live="polite" aria-relevant="all"><tr class="row12 odd">
    <td class=" sorting_1">test admin</td>
    <td class="mail ">gdfhfkhfflhh@fhfhfh.com</td>
    <td class="center ">2013-07-30 17:09:27</td>
    <td class="center ">2013-07-30 17:09:27</td>    
    <td class="center ">Yes</td>

    <td class=" "><a class="point" onclick="edit(12)">Edit</a> &nbsp; 
     &nbsp; <input class="multi-select checkbox" type="checkbox" uid="12">
    <i></i></td>
    </tr><tr class="row8 even">
    <td class=" sorting_1">test neeharika</td>
    <td class="mail ">jiouu@yahoo.com</td>
    <td class="center ">2013-07-30 12:31:06</td>
    <td class="center ">2013-07-30 12:31:06</td>    
    <td class="center ">Yes</td>

    <td class=" "><a class="point" onclick="edit(8)">Edit</a> &nbsp; 
     &nbsp; <input class="multi-select checkbox" type="checkbox" uid="8">
    <i></i></td>
    </tr><tr class="row11 odd">
    <td class=" sorting_1">test neeharika</td>
    <td class="mail ">gdfhfhfflhh@fhfhfh.com</td>
    <td class="center ">2013-07-30 14:05:14</td>
    <td class="center ">2013-07-30 14:05:14</td>    
    <td class="center ">Yes</td>

    <td class=" "><a class="point" onclick="edit(11)">Edit</a> &nbsp; 
     &nbsp; <input class="multi-select checkbox" type="checkbox" uid="11">
    <i></i></td>
    </tr><tr class="row4 even">
    <td class=" sorting_1">Tom Cruise</td>
    <td class="mail ">rohit@mail.com</td>
    <td class="center ">0000-00-00 00:00:00</td>
    <td class="center ">2013-09-05 13:08:32</td>    
    <td class="center ">Yes</td>

    <td class=" "><a class="point" onclick="edit(4)">Edit</a> &nbsp; 
     &nbsp; <input class="multi-select checkbox" type="checkbox" uid="4">
    <i></i></td>
</tr></tbody></table>

好的,这就是所有过程 -

  1. 用户选中任何复选框。
  2. 用户单击 del 按钮。
  3. 对话框打开了。
  4. 我们查看是否选中了任何复选框。
  5. 然后,如果选中任何复选框,我们会从属性中获取 taht 用户的 id。
  6. 调用 del() 删除该用户。

这是打开需要计算复选框的对话框的代码-

$(document).on("click", "#del", function () {
$(function () {
    $("#delete-user").dialog({
        autoOpen: false,
        height: 'auto',
        width: 'auto',
        modal: true

    });
    $("#none_selected").dialog({
        autoOpen: false,
        height: 'auto',
        width: 'auto',
        modal: true

    });
});
var checked =countchecked();
if(checked !="0")
{
$("#delete-user").dialog("open");
uid = $(this).attr("uid");
}
else
{
$("#none_selected").dialog("open");
uid = $(this).attr("uid");
}

 });
4

2 回答 2

0

只计算复选框可以通过 .filter 函数简化

$('tbody[role="alert"] input:checkbox').filter(':checked').length

然后循环检查项目

$('tbody[role="alert"] input:checkbox').filter(':checked').each(function(){
  //.. $(this) is the checkbox
});
于 2013-09-07T08:43:56.223 回答
-1

对于动态添加的控件,您必须将事件处理函数委托给控件。

可以.on()在jquery中使用

看看:http ://api.jquery.com/on/

例如:

$( '#wrapper' ).on( 'click', 'a', function () { ... });

wrapper你的静态元素在哪里,a你的动态元素在哪里

喜欢 : <table id="wrapper"></table>

于 2013-09-07T08:22:22.910 回答