jquery通过prop来对checkbox进行全选

来源:互联网 发布:软件测试人员培训 编辑:程序博客网 时间:2024/05/07 18:44

jquery判断checked的三种方法:

.attr(‘checked’):   //看版本1.6+返回:”checked”或”undefined” ;1.5-返回:true或false
.prop(‘checked’): //16+:true/false
.is(‘:checked’):    //所有版本:true/false

jquery赋值checked的几种写法:

所有的jquery版本都可以这样赋值:

// $(“#cb1″).attr(“checked”,”checked”);
// $(“#cb1″).attr(“checked”,true);

jquery1.6+:prop的4种赋值:

// $(“#cb1″).prop(“checked”,true);
// $(“#cb1″).prop({checked:true}); //map键值对
// $(“#cb1″).prop(“checked”,function(){
return true;//函数返回true或false
});

$(“#cb1″).prop(“checked”,”checked”);


function chAll(ch_id){
       if($('input[name="ck_all"]').prop("checked")){
           $('input[name="'+ch_id+'"]').prop("checked",true);
       }else{
           $('input[name="'+ch_id+'"]').prop("checked",false);
       }
       
   }


<table class="table table-hover table-bordered">
               <thead>
                 <th width="4%"><input type="checkbox" name="ck_all" onclick="chAll('ck_id');"/></th>
                 <th width="5%">序号</th>
                 <th width="20%">姓名</th>
                 <th width="10%">年龄</th>
                 <th width="10%">性别</th>
                 <th width="30%">备注</th>
                 <th width="10%">操作</th>
               </thead>
               <tbody>
                 <c:forEach items="${page.list}" var="user" varStatus="status">
                 <tr>
                    <td><input type="checkbox" name="ck_id" value="${user.id}"/></td>
                    <td>${(status.index+1)+page.pageSize*(page.pageNumber-1)}</td>
                    <td><a href="javascript:void(0);" onclick="jumpToFrame('/user/show/${user.id}-readonly');">${user.name }</a></td>
                    <td>${user.age }</td>
                    <td>${user.sex==1?'男':'女' }</td>
                    <td>${user.remark }</td>
                    <td>
                      <button type="button" class="btn btn-primary btn-sm" onclick="locationHref('${ctx}/user/show/${user.id }');">修改</button>
                      <button type="button" class="btn btn-danger btn-sm" onclick="locationHref('${ctx}/user/del/${user.id }');">删除</button>
                 </tr>
                 </c:forEach>
               </tbody>
            </table>
0 0
原创粉丝点击