关于选择单选按钮的确认

时间:2014-05-11 10:51:23

标签: javascript jquery html5

如何通过调用javascript的内联函数来检查是否选中了单选按钮?

<html>
<head>
<title>Test this</title>
<script>
var cnfrm= function(){
var x=document.getElementById('rdio2').value;
if (x==null || x=="")
    {
       confirm('are you sure?');
       // return false;
      // redirect to where you want
    }

   }
</script>
</head>
<body>
<form method=post action=index.html>
<br><input type=radio name=rdio id=rdio2 value=b>
<br><input type=radio name=rdio id=rdio2 value=c>
<br>
<input type=submit value=Submit onclick=cnfrm()>
</form>
</body>
</html>

现在我没有得到任何东西,它忽略cnfrm函数,所以我希望它能够执行,以便我可以检查天气是否被选中。

1 个答案:

答案 0 :(得分:1)

只需使用.checked代替.value

  if(document.getElementById('rdio2').checked==true) {
    return true;
  } else if(document.getElementById('rdio3').checked==true) {
    return true;
  } else if(document.getElementById('rdio4').checked==true) {
    return true;
  }  else {
    return confirm('are you sure?');
  }

FIDDLE DEMO

相关问题