CheckBox全选

来源:互联网 发布:android 源码 github 编辑:程序博客网 时间:2024/06/14 01:12

这里写图片描述
这里实现的效果是:
当点击选择的时候,显示一个div,上面有许多checkbox,
当点击全选的时候,会选中所有的checkbox。
遇到的问题:
在使用Jquery的attr做属性设置的时候,第一次全选和取消全选起作用,之后就不起作用,修复方法,使用prop函数。
具体如下:
这里写图片描述

<td class='tdEr' style="position:relative" id="InterType">                            <div id="TypeSelect"                             style="cursor:pointer;width:100px;background:#CBC9C5;text-align:center;border-radius:5px;">选择</div>                            <div id="TypeDialog"                             style="width:200px;position:absolute;left:10px;top:30px;background:#DFDFDC;border-radius:10px;                                padding-left:10px;padding-top:10px;border:1px solid #C4C4C2;">                            </div>                              </div>                         </td>

//选择接口类型
(“#TypeSelect”).click(function(){.ajax({
url: ‘${root}/statisticalAnalysis/002/listAllInterType.do’,
type : ‘post’,
data:{},
dataType : ‘text’,
success : function(data){
var dataStr=data.substr(1,data.length-2);
var arr=dataStr.split(“,”);

                    var str='';                    str+='<input id="checkAll" type="checkbox" style="margin:0 5px 0 10px;"/>';                    str+='<label for="checkAll">[@s.m 'allSelect'/]</label><br/>';                    for(var i=0;i<arr.length;i++){                        str+='<input id="abc'+i+'" type="checkbox" name="interType" value="'+arr[i].trim()+'" style="margin:0 5px 0 10px;"/>';                        str+='<label for="abc'+i+'">'+arr[i].trim()+'</label>';                        str+="<br/>";                    }                    str+='<input type="button" value="[@s.m 'confirm2'/]" class="inputTwo inputQuery"  id="confirmBut" style="position:absolute;bottom:5px;right:20px;"/>';                    $("#TypeDialog").empty();                    $("#TypeDialog").append(str);                    if(flag){                        $("#TypeDialog").height($("#TypeDialog").height()+35);                        flag=false;                    }                    $("#TypeDialog").show();                    $("#confirmBut").click(function(){                        $("#TypeDialog").hide();                     });                    $("#checkAll").click(function(){                        if($('#checkAll').is(':checked')) {                            $("#TypeDialog input[type='checkbox']").prop("checked",true);//注意这里                        }else{                            $("#TypeDialog input[type='checkbox']").prop("checked",false);                        }                    });                },                 error : function(data){                    alert("[@s.m 'notice103'/]");                 }             });      });  })

“`
当点击”选择”的时候,会发送ajax请求到后台加载checkbox的内容,这里代码有关于业务,标签是freemarker的,就不做过多解释,然后将后台获取到的数据做拼接后动态添加到DIV弹出框中。

原创粉丝点击