2 回答
TA贡献1817条经验 获得超14个赞
<
input
type
=
"checkbox"
data-index
=
"1"
class
=
"chexkbox"
>选项1
<
input
type
=
"checkbox"
data-index
=
"2"
class
=
"chexkbox"
>选项2
<
input
type
=
"checkbox"
data-index
=
"3"
class
=
"chexkbox"
>选项3
<
input
type
=
"checkbox"
data-index
=
"4"
class
=
"chexkbox"
>选项4
<
input
type
=
"checkbox"
data-index
=
"5"
class
=
"chexkbox"
>选项5
<
input
type
=
"checkbox"
data-index
=
"6"
class
=
"chexkbox"
>选项6
<
input
type
=
"checkbox"
data-index
=
"7"
class
=
"chexkbox"
>选项7
<
input
type
=
"checkbox"
data-index
=
"8"
class
=
"chexkbox"
>选项8
<
input
type
=
"checkbox"
data-index
=
"9"
class
=
"chexkbox"
>选项9
<
input
type
=
"checkbox"
data-index
=
"10"
class
=
"chexkbox"
>选项10
<
script
>
var checkboxArr = document.getElementsByClassName('chexkbox');
for(var i = 0; i < checkboxArr.length; i ++){
checkboxArr[i].onclick = function(){
var index = this.getAttribute('data-index');
for(var c = 0; c < index; c ++){
if(this.checked == true){
checkboxArr[c].checked = true;
}else{
checkboxArr[c].checked = false;
}
}
}
}
</
script
>
TA贡献1785条经验 获得超8个赞
用jquery写的。用到了jquery的prevAll()方法。
$(".input").click(function(){//这个input是我给checkbox取的共有类名
$(this).prevAll().attr("checked",true);
})
添加回答
举报