我已經在這裏梳理了大量頁面,並且仍然無法讓我的顯式驗證器正常工作。基本上,當點擊提交按鈕時,我希望腳本驗證一個收音機是否被選中,如果有人被選中則什麼也不做。如果沒有選中,我希望它發佈警報消息。使用Javascript驗證單選按鈕
大概我的HTML看起來像:
<form id="myForm" onsubmit = "check()">
<input type = "radio" name = "c" id = "1" value = "1" />
<input type = "radio" name = "c" id = "2" value = "2" />
<input type = "radio" name = "c" id = "3" value = "3" />
<input type = "submit" value = "Submit" />
我的JS網頁的樣子:
function check() {
var r = document.getElementsByName("c")
var c = 0
for(var i=0; i < r.length; i++){
if(c[i].checked) {
c = i; }
}
alert("please select radio");
}
http://www.chennaisunday.com/jsradio.html – 2015-02-03 05:50:43