我已经在这里梳理了大量页面,并且仍然无法让我的显式验证器正常工作。基本上,当点击提交按钮时,我希望脚本验证一个收音机是否被选中,如果有人被选中则什么也不做。如果没有选中,我希望它发布警报消息。使用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