How to loop through a radio buttons group without a form?

后端 未结 3 1442
死守一世寂寞
死守一世寂寞 2021-02-02 12:44

How do I loop through a radio buttons group without a form in JavaScript or jQuery?

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-02 13:34

    What about something like this? (using jQuery):

    $('input:radio').each(function() {
      if($(this).is(':checked')) {
        // You have a checked radio button here...
      } 
      else {
        // Or an unchecked one here...
      }
    });
    

    You can also loop through all the checked radio buttons like this, if you prefer:

    $('input:radio:checked').each(function() {
       // Iterate through all checked radio buttons
    });
    

提交回复
热议问题