我有一個有多個複選框的表單,我想使用JavaScript來確保至少有一個選中。這就是我現在所擁有的,但無論選擇什麼,都會彈出一個警報。確保至少有一個複選框被選中
JS(錯誤)
function valthis(){
if (document.FC.c1.checked) {
alert ("thank you for checking a checkbox")
} else {
alert ("please check a checkbox")
}
}
HTML
<p>Please select at least one Checkbox</p>
<br>
<br>
<form name = "FC">
<input type = "checkbox" name = "c1" value = "c1"/> C1
<br>
<input type = "checkbox" name = "c1" value = "c2"/> C2
<br>
<input type = "checkbox" name = "c1" value = "c3"/> C3
<br>
<input type = "checkbox" name = "c1" value = "c4"/> C4
<br>
</form>
<br>
<br>
<input type = "button" value = "Edit and Report" onClick = "valthisform();">
因此,我結束了在JS做的是這樣的:
function valthisform(){
var chkd = document.FC.c1.checked || document.FC.c2.checked||document.FC.c3.checked|| document.FC.c4.checked
if (chkd == true){
} else {
alert ("please check a checkbox")
}
}
我決定放棄「謝謝」部分以適應作業的其餘部分。非常感謝你,每一個建議真的幫了大忙。
爲什麼在表單的「編輯和報告」按鈕*外部* – 2012-08-03 00:43:31