2016-09-08 35 views
0

我在我的表单中有许多单选按钮,具有不同的名称组。我得到单个无线电组值,但我想获得表单的所有组单选按钮值。 这里是我的代码,我得到反馈单选按钮值,而不是状态。请帮帮我。在此先感谢无法获得多个单选按钮的价值

<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/

+0

'getElementsByName()'只接受一个字符串参数 –

+0

document.getElementsByName接受一个参数,因此 “状态” 被忽略...尝试querySelectorAll作为替代 –

回答

0

如果你可以在这里包含Jquery的解决方案。

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()); 
 
    } 
 
    } 
 
    }
<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>

0

你可能要检查你的feedle。如果你想打印所有的单选按钮val,你只需要从radioButtons [x] .checked条件中获取警报。但是,如果只想在条件满足的情况下打印所有的值,那么你可能会这样做feedle