2016-09-08 37 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