无法获得多个单选按钮的值

时间:2016-09-08 14:56:25

标签: javascript

我的表格中有很多单选按钮,不同的名字组。我得到单个无线电组值,但我想得到表单的所有组单选按钮值。 这是我的代码,我得到反馈单选按钮值,而不是状态。请帮我。提前致谢

<html>
<head>
<script type="text/javascript">
function myFunction() {
     var radioButtons = document.getElementsByName("feedback","status");
    for (var x = 0; x < radioButtons.length; x ++) {
      if (radioButtons[x].checked) {
       alert("You checked " + radioButtons[x].id);
       alert("Value is " + radioButtons[x].value);
     }
     }
  }
    </script>
</head>
<body>
<input type="radio" name="feedback" id="outstanding" value="1" />Outstanding
<input type="radio" name="feedback" id="good" value="2" />Good
<input type="radio" name="feedback" id="accept" value="3" />Acceptable  
<input type="radio" name="status" id="done" value="1" />Done
<input type="radio" name="status" id="nothing" value="2" />Nothing
<input type="radio" name="status" id="work" value="3" />Work    
<button class="btn btn-primary pull-right place-order-button" name="submit" value="submit" onclick="myFunction()" type="submit" >Finish</button>
</body>
</html>

https://fiddle.jshell.net/nady/711evwd8/

2 个答案:

答案 0 :(得分:0)

您可能想查看Feed。如果你想打印所有单选按钮val,你只需要从radioButtons [x] .checked条件中获取警报。然而,如果你想只在符合条件的情况下打印所有值,你就会做到这样的事情feedle

答案 1 :(得分:0)

如果你能在这里包含Jquery就是解决方案。

&#13;
&#13;
function myFunction() {
 
     var radioButtons =["feedback","status"]; 
    for (var x = 0; x < radioButtons.length; x ++) {
      if ($('[name = "'+radioButtons[x]+'"]:checked')) {
       alert("You checked " + $('[name = "'+radioButtons[x]+'"]:checked').attr('id'));
       alert("Value is " + $('[name = "'+radioButtons[x]+'"]:checked').val());
     }
     }
  }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
<script type="text/javascript">

    </script>
</head>
<body>
<input type="radio" name="feedback" id="outstanding" value="1" />Outstanding
<input type="radio" name="feedback" id="good" value="2" />Good
<input type="radio" name="feedback" id="accept" value="3" />Acceptable  
<input type="radio" name="status" id="done" value="1" />Done
<input type="radio" name="status" id="nothing" value="2" />Nothing
<input type="radio" name="status" id="work" value="3" />Work    
<button class="btn btn-primary pull-right place-order-button" name="submit" value="submit" onclick="myFunction()" type="submit" >Finish</button>
</body>
</html>
&#13;
&#13;
&#13;