使用Javascript验证单选按钮

时间:2013-02-08 19:59:24

标签: javascript radio-button validation

我已经在这里梳理了大量的页面,但仍然无法让我的显式验证器工作。基本上,当单击提交按钮时,我希望脚本验证是否检查了无线电,如果检查了无线电。如果没有选中,我希望它发布一条警告信息。

大致上我的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");
}

5 个答案:

答案 0 :(得分:4)

尝试这个

function check() {
var r = document.getElementsByName("c")
var c = -1

for(var i=0; i < r.length; i++){
   if(r[i].checked) {
      c = i; 
   }
}
if (c == -1) alert("please select radio");
}

答案 1 :(得分:1)

c[i].check

应该是

c[i].checked

并且你实际上并没有对结果做任何事情,你总是在提醒。

答案 2 :(得分:0)

<html>
<head>
<script language="javascript">
function check()    {

chosen = ""
len = document.myform.chk.length

for (i = 0; i <len; i++) {
if (document.myform.chk[i].checked) {
chosen = document.myform.chk[i].value
}
}

if (chosen == "") {
alert("No Option selected");
return false;
}
else {
alert("option selected");
return true;
}
}
</script>
</head>
<body>
<form name="myform" onsubmit = "return check();">
<input type = "radio" name = "chk" id = "1" value = "1" >
<input type = "radio" name = "chk" id = "2" value = "2" >
<input type = "radio" name = "chk" id = "3" value = "3" >

<input type="submit" value="submit">

</form>
</body>
</html>

检查此验证希望它也可能对您有所帮助。同时在jsfiddle

中查看

答案 3 :(得分:0)

             function ShowMsg() {    

             if (fnSpeciality() == false) 
              {
              document.getElementById("myform").focus();
               return false;
            }

                function fnSpeciality() 
                {
             return fnRblfnSpeciality();
                 }
              function fnSpeciality() {
                   return fnRblfnSpeciality();
                          }


    function fnRblfnSpeciality() {

        var list = document.getElementById('myform'); //Client ID of the radiolist
        var inputs = list.getElementsByTagName("input");
        var isItemChecked = false;
        for (var i = 0; i < inputs.length; i++) {
            var listItem = inputs[i];

            if (listItem.checked) {
                //alert(listItem.value);
                isItemChecked = true;
                break;
            }
        }
     if (isItemChecked == false) {
            if (isItemChecked =="") {
                alert('Please select a speciality.');
                return false;
            }
            // else return true;
        }
        return true;
    }

答案 4 :(得分:-1)

                                                                                                                                             信用卡                             借记卡                             按现金
                        

                </td>
            </tr>

如果对此单选按钮进行了java脚本验证,则使用单选按钮列表..pls共享