为什么给复选框绑定click事件,只成功执行一次,后续就不行了呢?选不中下面的。
要实现效果:点击第一行复选框,对应的下面的复选框也被选中,取消选中,对应的也取消选中。
测试结果:第一次执行可以成功选中和取消选中,以后就选不中了。
代码截图:
第一次成功:

以后不成功:

代码:
html:
-
-
-
jquery部分:
$(document).ready(function(){
var curTr = $('.qx-ul > li');
var curparInput = curTr.find('p > input');
//console.log(curparInput);
//点击父元素 全选子元素
curparInput.click(function () {
console.log('hello');
var curIndex = $(this).index();
var cursonInput = $(this).parent().next().find('input');
if($(this).is(':checked') == true){
console.log('执行了!');
console.log( '执行了,当前索引是' + curIndex);
console.log(cursonInput);
$(cursonInput[curIndex - 1]).attr('checked',true);
$(cursonInput[curIndex + 4]).attr('checked',true);
}else{
console.log('执行了!+1');
console.log( '执行了+1,当前索引是' + curIndex );
console.log(cursonInput);
$(cursonInput[curIndex - 1]).attr('checked',false);
$(cursonInput[curIndex + 4]).attr('checked',false);
}
});
});
求大神指导~。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
把 attr 方法替换成 prop
http://jsfiddle.net/ws9j4v1v/11/
没错下次可以记住,复选框用户prop,而不是attr
我试过了用prop就可以,但是为什么用attr不行呢???请各位大神解答,本人小白