2 回答

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
<
input
type
=
"checkbox"
data-index
=
"1"
class
=
"chexkbox"
>選項(xiàng)1
<
input
type
=
"checkbox"
data-index
=
"2"
class
=
"chexkbox"
>選項(xiàng)2
<
input
type
=
"checkbox"
data-index
=
"3"
class
=
"chexkbox"
>選項(xiàng)3
<
input
type
=
"checkbox"
data-index
=
"4"
class
=
"chexkbox"
>選項(xiàng)4
<
input
type
=
"checkbox"
data-index
=
"5"
class
=
"chexkbox"
>選項(xiàng)5
<
input
type
=
"checkbox"
data-index
=
"6"
class
=
"chexkbox"
>選項(xiàng)6
<
input
type
=
"checkbox"
data-index
=
"7"
class
=
"chexkbox"
>選項(xiàng)7
<
input
type
=
"checkbox"
data-index
=
"8"
class
=
"chexkbox"
>選項(xiàng)8
<
input
type
=
"checkbox"
data-index
=
"9"
class
=
"chexkbox"
>選項(xiàng)9
<
input
type
=
"checkbox"
data-index
=
"10"
class
=
"chexkbox"
>選項(xiàng)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貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
用jquery寫(xiě)的。用到了jquery的prevAll()方法。
$(".input").click(function(){//這個(gè)input是我給checkbox取的共有類(lèi)名
$(this).prevAll().attr("checked",true);
})
添加回答
舉報(bào)