html部分代码:
<tr>
<th class="text-center" id="choose"><input type="checkbox" id="checked_all" /></th>
</tr>
. . .
<tr>
<td class="text-center"><input type="checkbox" class="checked_pro" name="checkbox" /></td>
</tr>
js脚本:
function checkedAction(){
if($("#checked_all").is(":checked")){
$(".checked_pro").attr("checked","checked");
}else{
$(".checked_pro").removeAttr("checked");
}
}
发现全选没有任何效果。
在Chrome浏览器控制台上看到的源码却多了一些标签:
<tr>
<td class="text-center">
<div class="checker">
<span class="">
<input type="checkbox" class="checked_pro" name="checkbox" />
</span>
</div>
</td>
</tr>
所以修改js脚本即可:
function checkedAction(){
$("input[type='checkbox']").each(function() {
if($("#checked_all").is(":checked")) {
$(".checked_per_pro").attr("checked","checked");
$(this).parent().addClass("checked");
}else{
$(".checked_per_pro").removeAttr("checked");
$(this).parent().removeClass("checked");
}
});
}
本文介绍了在Bootstrap环境下,遇到全选checkbox功能失效的问题及其解决方案。通过分析Chrome浏览器控制台显示的源码,发现多出的`<div class="checker">`标签导致了功能异常。修复方法是更新js脚本,使用`$(this).parent().addClass/removeClass("checked")`来处理选中状态。
1280

被折叠的 条评论
为什么被折叠?



