如何在没有表单的情况下循环单选按钮组?

2022-01-21 00:00:00 radio-button jquery javascript html

如何在 JavaScript 或 jQuery 中循环遍历没有表单的单选按钮组?

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

推荐答案

这样的事情怎么办?(使用 jQuery):

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
});

相关文章