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);
})
添加回答
舉報