【问题标题】:Confirmation about selection of radio Button [duplicate]关于选择单选按钮的确认[重复]
【发布时间】:2014-06-28 19:00:06
【问题描述】:

如何通过调用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 函数,所以我希望它执行,以便我可以检查天气它是否被选中。

【问题讨论】:

  • 您的 HTML 标记无效,ID 在文档上下文中必须是唯一的
  • @A.Wolff——ID 无论如何都是多余的。

标签: javascript jquery html


【解决方案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

【讨论】:

  • 如果我选择底部单选按钮,它就不能正常工作
猜你喜欢
  • 2016-12-17
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多