简体   繁体   English

Jquery - 遍历所有选中的单选按钮

[英]Jquery - Iterate through all checked radio buttons

I have a form which is similar like the one below: 我有一个类似下面的表格:

<form id="myForm">
      Question 1:<br>
      <input type="radio" name="question1" value="Yes"> Yes
      <input type="radio" name="question1" value="No"> No
      <br>
      Question 2:<br>
      <input type="radio" name="question2" value="Yes"> Yes
      <input type="radio" name="question2" value="No"> No
      <br>
      Question 3:<br>
      <input type="radio" name="question3" value="Yes"> Yes
      <input type="radio" name="question3" value="No"> No
      <br>
      <input type="submit" value="Submit" name="submit">
</form>

I want to get all the selected radio button values from all the questions using jQuery and if all values is equal to "yes", it will alert success else it will alert fail. 我想使用jQuery从所有问题中获取所有选定的单选按钮值,如果所有值都等于“是”,它将提示成功,否则它将提示失败。

This is the jQuery I wrote: 这是我写的jQuery:

$(document).ready(function(){
   $("#myForm input[type=radio]:checked").each(function() {
       var value = $(this).val();
   });
});

You can check if you ever get no with radio checked then result is fail else success. 您可以检查您是否收到无线电检查,然后结果失败,否则成功。

Live Demo 现场演示

result = "success";
$("#myForm input[type=radio]:checked").each(function() {
  if(this.value == "No" && this.checked == true)
  {
     result = "fail";
     return false;
  }
});
alert(result);  
   $(document).ready(function(){
     var val=1;
       $("#myForm input[type=radio]:checked").each(function() {
            if($(this).val()=="No")
             {
              val=2;
             }
       });

      if(val==1)
      {
        alert("Success !!");
      }
      else{
        alert("Fail ");
      }
    });
$(document).ready(function(){
   var no_found=false;
   $("#myForm").submit(function(){
      $(this).find("input[type=radio]:checked").each(function() {
       var value = $(this).val();
       if(value == "No")
           no_found=true;
      });
      if(no_found==false){
       alert("Success");
      } 
      else{
       alert("Fail");
      }
   });
});

Use this code: 使用此代码:

 $(function() { $('#myForm').on('submit', function(e) { e.preventDefault(); var total = 0; $('#myForm input[type="radio"]:checked').each(function() { if ($(this).val() == 'Yes') total++; }); if (total == 3) alert("Success"); else alert("Fail"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="myForm"> Question 1: <br> <input type="radio" name="question1" value="Yes" checked>Yes <input type="radio" name="question1" value="No">No <br>Question 2: <br> <input type="radio" name="question2" value="Yes">Yes <input type="radio" name="question2" value="No" checked>No <br>Question 3: <br> <input type="radio" name="question3" value="Yes">Yes <input type="radio" name="question3" value="No" checked>No <br> <input type="submit" value="Submit" name="submit"> </form> 

Try this code 试试这个代码

$("#myForm").submit(function(){
    var check = "Yes";
    $(this).find("input[type=radio]:checked").each(function() {
     var value = $(this).val();
     if(value == "No")
      check = "No";
    });
    alert(check)
  });

You may try this as well: 你也可以试试这个:

http://codepen.io/anon/pen/JGeLMx http://codepen.io/anon/pen/JGeLMx

$('#myForm input[type="submit"]').on('click', function(e) {

  e.preventDefault();  

  var result = true;  
  $('input[type="radio"]').each( function() {

    var radio = $(this)[0];    
    if ( radio.value === 'Yes' && radio.checked === false )
      result = false;

  });

  if ( result ) {
    alert( 'Success!' );
  } else {
    alert( 'Fail!' );
  }

});

Iterated all the radio buttons and if a single check on 'No' or none is selected at all it will fail, otherwise, it would mean all 'Yes' are checked - success. 迭代所有的单选按钮,如果单独检查“否”或根本没有选择它将失败,否则,这将意味着所有“是”都被检查 - 成功。

<!DOCTYPE html>
<html>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>


<script>
    $(document).ready(function () {

        $('input[type="radio"]').change(function () {
            var iCountTotal;
            var iCountCkedYes;
            iCountTotal = $('input[type="radio"]').length/2;
            iCountCkedYes = $('input[type="radio"][value="Yes"]:checked').length;
            if (iCountTotal != iCountCkedYes) {
                alert('fail')
            }
            else {
                alert('success')

            }
        });


    });


</script>



<body>

    <form id="myForm">
      Question 1:<br>
      <input type="radio" name="question1" value="Yes" checked="checked"> Yes
      <input type="radio" name="question1" value="No"> No
      <br>
      Question 2:<br>
      <input type="radio" name="question2" value="Yes"> Yes
      <input type="radio" name="question2" value="No" checked="checked"> No
      <br>
      Question 3:<br>
      <input type="radio" name="question3" value="Yes"> Yes
      <input type="radio" name="question3" value="No" checked="checked"> No
      <br>
      <input type="submit" value="Submit" name="submit">
</form>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM