我已经在这里梳理了很多页面,但仍然无法让我的显式验证器工作。基本上,当单击提交按钮时,我希望脚本验证一个单选是否被选中,如果一个被选中则什么都不做。如果未选中,我希望它发布一条警报消息。
大致我的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");
}