最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I check if all radio buttons are checked - Stack Overflow

programmeradmin3浏览0评论

I have a number of radio buttons with the option, 'yes' or 'no'.

Is there a simple way with jQuery to check if they all have 'yes' selected?

HTML is:

<input type="radio" id="yes1" name="group1" value="yes">Yes<br>
<input type="radio" name="group1" value="No">No<br>
<hr>
<input type="radio" id="yes2" name="group2" value="yes">Yes<br>
<input type="radio" name="group2" value="No">No<br>
<hr>
<input type="radio" id="yes3" name="group3" value="yes">Yes<br>
<input type="radio" name="group3" value="No">No<br>

I'm guessing it's something along the lines of

yes1 = $("#yes1").prop("checked", true);
yes2 = $("#yes2").prop("checked", true);
yes3 = $("#yes2").prop("checked", true);

if (yes1 & yes2 & yes3) {
   // do something ?
}

I have a number of radio buttons with the option, 'yes' or 'no'.

Is there a simple way with jQuery to check if they all have 'yes' selected?

HTML is:

<input type="radio" id="yes1" name="group1" value="yes">Yes<br>
<input type="radio" name="group1" value="No">No<br>
<hr>
<input type="radio" id="yes2" name="group2" value="yes">Yes<br>
<input type="radio" name="group2" value="No">No<br>
<hr>
<input type="radio" id="yes3" name="group3" value="yes">Yes<br>
<input type="radio" name="group3" value="No">No<br>

I'm guessing it's something along the lines of

yes1 = $("#yes1").prop("checked", true);
yes2 = $("#yes2").prop("checked", true);
yes3 = $("#yes2").prop("checked", true);

if (yes1 & yes2 & yes3) {
   // do something ?
}
Share Improve this question edited Jan 27, 2015 at 10:19 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Jan 27, 2015 at 10:18 user2726041user2726041 3595 silver badges21 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 5

You can rather pare the length of elements with ['value=yes] with elements with ['value=yes] and property :checked:

 if($('[value=yes]').length==$('[value=yes]:checked').length){
     //all yes elements are checked
 }

One way is to check whether all the radios with value as yes is checked

if($('input[type="radio"][value="yes"]').not(':checked').length == 0){
    //all checked
}

You may check the count of radio buttons with value != true. If the count is Zero, all radio buttons would be selected.

if(!$('input[type="radio"][value="yes"]').not(':checked').length){
//select all radio buttons with value = yes
}

<!DOCTYPE html>
<html>

<head>
  <title></title>

</head>

<body>
  <input type="radio" id="yes1" name="group1" value="yes">Yes
  <br>
  <input type="radio" name="group1" value="No">No
  <br>
  <hr>
  <input type="radio" id="yes2" name="group2" value="yes">Yes
  <br>
  <input type="radio" name="group2" value="No">No
  <br>
  <hr>
  <input type="radio" id="yes3" name="group3" value="yes">Yes
  <br>
  <input type="radio" name="group3" value="No">No
  <br>
  <input type="button" id="btn" value="Test" />

  <script src="https://ajax.googleapis./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("#btn").click(function() {
        var length = 3;
        var isChecked = true;
        for (var i = 1; i <= length; i++) {
          isChecked = isChecked && ($("#yes" + i).is(":checked"));
        }
        if (isChecked)
          alert("All are checked");
        else
          alert("All are not checked");
      });
    });
  </script>

</body>

</html>

<input type="radio" id="yes1" name="group1" value="yes" checked>Yes<br>
<input type="radio" name="group1" value="No">No<br>
<hr>
<input type="radio" id="yes2" name="group2" value="yes" checked>Yes<br>
<input type="radio" name="group2" value="No">No<br>
<hr>
<input type="radio" id="yes3" name="group3" value="yes" checked>Yes<br>
<input type="radio" name="group3" value="No">No<br>


var yes1 = $("#yes1").is(":checked")
    var yes2 = $("#yes2").is(":checked")
    var yes3 = $("#yes3").is(":checked")
    //$("#Myradio").is(":checked")

    if (yes1 & yes2 & yes3) {
        alert('sasa');
       //do something
    }

FIDDLE

<!DOCTYPE html>
<html>

<head>
  <title></title>

</head>

<body>
  <input type="radio" id="yes1" name="group1" value="yes">Yes
  <br>
  <input type="radio" name="group1" value="No">No
  <br>
  <hr>
  <input type="radio" id="yes2" name="group2" value="yes">Yes
  <br>
  <input type="radio" name="group2" value="No">No
  <br>
  <hr>
  <input type="radio" id="yes3" name="group3" value="yes">Yes
  <br>
  <input type="radio" name="group3" value="No">No
  <br>
  <input type="button" id="btn" value="Test" />

  <script src="https://ajax.googleapis./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
      $("#btn").click(function() {
        var length = 3;
        var isChecked = true;
        for (var i = 1; i <= length; i++) {
          isChecked = isChecked && ($("#yes" + i).is(":checked"));
        }
        if (isChecked)
          alert("All are checked");
        else
          alert("All are not checked");
      });
    });
  </script>

</body>

</html>

发布评论

评论列表(0)

  1. 暂无评论