在MVC5中的Html表中选择或取消选中所有复选框 [英] Select or Deselect All checkboxes in Html table in MVC5

查看:79
本文介绍了在MVC5中的Html表中选择或取消选中所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的视图中有一个html表。我应用一个复选框来选中我表格的所有复选框。这是我的代码:

< table id =dtsResultclass =table table-striped table-borderedcellspacing =0width =100% > 
< thead>
< tr>
< th> SelectAll< input type =checkboxid =CheckAllRes/>< / th>
< th>单位< / th>
< / tr>
< / thead>
< tbody>
@foreach(ViewBag.Result中的var项目)
{
< tr>
< td>< input type =checkboxid = @item.RecordID name =resclass =chkres/>< / td>
< td> @ item.UnitNo< / TD>
< / tr>
}
< / tbody>
< / table>
这是我的Jquery函数
$(document).ready(function(){
$('#CheckAllRes')。on(click,function(){
var chk = $(this).is(':checked');
$(。chkres,#dtsResult)。each(function(){
if(chk){
$(this).attr(checked,true);
}
else {
$(this).attr(checked,false);
}
});
});
});



现在问题是当我第一次点击它然后检查所有记录同样的删除。但当我再次点击选择所有。然后没有选中复选框。

我是jquery的新手。请帮忙。

谢谢

解决方案

(document).ready(function(){


('#CheckAllRes')。on(click,function(){
var chk =


(this).is(':checked');

Hi,
I have a html table in my view. I apply a checkbox to select all check boxes of my table. here is my code :

 <table id="dtsResult" class="table table-striped table-bordered" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>SelectAll<input type="checkbox" id="CheckAllRes" /></th>
                <th>Unit</th>
             </tr>
        </thead>
        <tbody>
            @foreach (var item in ViewBag.Result)
            {
                <tr>
                    <td><input type="checkbox" id=@item.RecordID name="res" class="chkres" /></td>                   
                    <td> @item.UnitNo</td>
                </tr>
            }
        </tbody>
    </table>
Here is my Jquery Function
 $(document).ready(function () { 
          $('#CheckAllRes').on("click", function (){          
            var chk = $(this).is(':checked');            
            $(".chkres", "#dtsResult").each(function () {
                if (chk) {                   
                    $(this).attr("checked", true);
                }
                else {
                    $(this).attr("checked", false);
                }
            });
        });
  });


Now my problem is when i click first time then it checks all records and same for remove. But when i click again to select all . then no checkbox is selected.
i am new to jquery. Please help.
Thanks

解决方案

(document).ready(function () {


('#CheckAllRes').on("click", function (){ var chk =


(this).is(':checked');


这篇关于在MVC5中的Html表中选择或取消选中所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆