我剛開始使用JavaScript,所以請原諒我,如果我的問題似乎很愚蠢。我想設計一個帶有一些複選框的網頁,當第一個複選框被選中時,其他複選框變爲啓用。我如何使用JS做到這一點?我寫了下面的代碼:使用Javascript啓用複選框
function checkBox() {
if (window.document.form1.mainbox.checked=true) {
for (i=0;i<window.document.form1.Others.length;i++) {
window.document.form1.Others[i].disabled=false;
}
}
else {
for (i=0;i<window.document.form1.Others.length;i++) {
window.document.form1.Others[i].disabled=true;
}
}
}
而且這個網站:
<form name="form1">
<input name="mainbox" value="mainbox" onclick="checkBox()" type="checkbox"> Mainbox<br>
<input disabled="disabled" checked="checked" tabindex="0" name="Others" type="checkbox">No. 1<br>
<input disabled="disabled" checked="checked" tabindex="1" name="Others" type="checkbox"> No. 2<br>
<input disabled="disabled" checked="checked" tabindex="2" name="Others" type="checkbox"> No. 3<br>
</form>
的問題是,在第一次點擊其他複選框都被啓用,但沒有發生在隨後的點擊,即它們做不再被禁用。我該如何糾正這個問題?任何幫助將不勝感激。謝謝!
編輯
我跟着gabitzish的建議和它的工作。不過,我現在想通過其他的參數,所以我寫:
<input name="mainbox" value="mainbox" onclick="checkBox(Others)" type="checkbox"> Mainbox<br>
和修改腳本如下:
window.checkBox = function(chkname) {
if (window.document.form1.mainbox.checked==true) {
for (i=0;i<window.document.form1.chkname.length;i++) {
window.document.form1.chkname[i].disabled=false;
}
}
else {
for (i=0;i<window.document.form1.chkname.length;i++) {
window.document.form1.chkname[i].disabled=true;
}
}
}
但是,這是行不通的。請幫我在這裏。我會非常感激。
謝謝!這正是我想要的! – user828647 2012-04-13 12:39:58
我編輯了我的答案,現在一個參數傳遞給您的函數 – gabitzish 2012-04-13 20:04:39
您懷疑是正確的,並且您的解決方案有效!再次感謝,我已經接受你答案的承諾:) – user828647 2012-04-14 11:42:56