2016-09-08 9 views
0

私はフォームに異なる名前のグループを持つラジオボタンが多数あります。私は単一のラジオグループの値を取得しますが、フォームのラジオボタンの値をすべて取得したいと思います。 ここで私はフィードバックラジオボタンの値ではなく、ステータスになっている私のコードです。私を助けてください。事前のおかげであなたはあなたのfeedleをチェックすることをお勧めします複数のラジオボタンの値を取得することができません

<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

。 radioボタン[x] .checked状態からアラートを取得するだけで、ラジオボタンのvalのすべてを印刷したい場合は、しかし、条件が満たされた場合にのみすべての値を出力したい場合は、次のようにします。feedle

関連する問題