簡體   English   中英

無法獲取多個單選按鈕的值

[英]unable to get value of multiple radio button

我的表單中有多個具有不同名稱組的單選按鈕。 我得到單個單選按鈕組值,但我想獲取表單的所有單選按鈕值。 這是我獲取反饋單選按鈕值而不是狀態的代碼。 請幫我。 提前致謝

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

您可能要檢查您的喂食。 如果要打印所有單選按鈕val,則只需要從radioButtons [x] .checked條件中獲取警報即可。 但是,如果您只想在滿足條件的情況下打印所有值,那么您將執行以下操作

如果可以包括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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM