代码要求,使用attr只能执行一次,使用prop可以完美的实现所有的选择和反选择,获取所有的选择项,并将选择项的文本组成一个字符串。
解决方案1:
代码如下:
html head script src=' http : jquery-1 . 11 . 1 . min . js ' type=' text/JavaScript '/script/head dyinput type=' checkbox ' name=' chk _ list[]' Value=' 1 '/1 input type=' checkbox ' name=' chk _ list[]' Value=' 2 '/2 input type=' checkbox ' name=' chk _ list[]' Value=' 3 '/3 input type=' checkbox ' name=' chk _ list '单击(function () {//您只能执行$ ('input [name=' chk _ list []']')。attr ('checked ',$ (this)。attr ('checked ')一次,使用attr;//使用prop完美选择并反转$ ('input [name=' chk _ list []']')。道具('选中',$(这个)。prop(' checked ');//获取所有选中的项目,并将选中项目的文本组成字符串var str=$($(' input[name=' chk _ list[]']: checked '))。每个(函数(){ str=$(this))。下一个()。text()',';});警报;});/script/body/html摘要:
对于HTML元素的固有属性,在处理时,使用prop方法。
对于我们自己定制的HTML元素的DOM属性,在处理的时候,使用attr方法。
见www.jb51.net/article/62308.htm。
解决方案2:
问题描述:
$('.choose all’)。单击(function(){ if($(')。choose all’)。attr(' checked ')==' checked '){ $(' input[name=' check box 1 ']')。removeAttr('选中','选中');console . log(1);} else { $(' input[name=' check box 1 ']')。attr('checked ',' checked ');console . log(2);}});
上面代码的第一次点击和第二次点击可以实现全部选择和反选择的功能,但是一次之后就不再起作用了。什么情况
除了第一个复选框,其他都是由ajax动态生成的。和这个有关系吗?每次console.log点击,可以交替输出1和2,但是中间代码无法执行。
解决方案:
只需要removeAttr的一个参数,建议将removeAttr('选中')替换为
$('.choose all’)。单击(function(){ if($(')。choose all’)。prop(' checked ')==true){ $(' input[name=' check box 1 ']')。prop('checked ',false);console . log(1);} else { $(' input[name=' check box 1 ']')。prop('checked ',false);console . log(2);}});或者更简洁地说,
$('.choose all’)。click(function(){ var isChecked=$(this))。道具('选中');$('input[name='checkbox1']')。道具('选中',isChecked);});
以上是Jquery全选反选的解决方案,点击执行一次。希望对大家有帮助。