确认选择单选按钮

Confirmation about selection of radio Button

本文关键字:单选按钮 选择 确认      更新时间:2023-09-26

如何通过调用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函数,所以我想让它执行,这样我就可以检查它是否被选中

.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