У меня есть много радио кнопок в моей форме с разными именами группы. Я получаю одно значение группы радиоканалов, но хочу получить все значения групповых радиокнопок в форме. Вот мой код, в котором указывается значение переключателя обратной связи, а не статус. пожалуйста, помогите мне. заранее спасибо

<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
nady gold 8 Сен 2016 в 17:56

2 ответа

Лучший ответ

Если вы можете включить 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
Jayababu 8 Сен 2016 в 15:22

Возможно, вы захотите проверить свой корм. Если вы хотите распечатать все радиокнопки val, вам просто нужно вывести предупреждение из условия radioButtons [x] .checked. Тем не менее, если бы вы хотели распечатать все значения только при соблюдении условия, вы бы сделали что-то вроде этого кормить

0
Josue Enamorado 8 Сен 2016 в 15:08