jQuery:循环 HTML 表中的所有单选按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2852052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:24:29  来源:igfitidea点击:

jQuery:Looping all radio buttons inside an HTML table

jqueryradio-button

提问by Shyju

I have an HTML table having n rows and each rows contain one radiobutton in the Row.Using jQuery , How can i look thru these radio buttons to check which one is checked ?

我有一个包含 n 行的 HTML 表,每一行在 Row.Using jQuery 中包含一个单选按钮,我如何查看这些单选按钮以检查哪个单选按钮被选中?

回答by Sunny

$('#table tbody tr input[type=radio]').each(function(){
 alert($(this).attr('checked'));
});

HTH.

哈。

回答by karim79

There are many ways to do that, e.g., using .eachand the .istraversal method:

有很多方法可以做到这一点,例如使用.each.is遍历方法:

$("table tbody tr td input[name=something]:radio").each(function() {
    if($(this).is(":checked")) {
        $(this).closest("tr").css("border", "1px solid red");
    } else {
        // do something else
    }
});

回答by SaiyanGirl

To loop through all the radio checked radio buttons, you can also do this:

要遍历所有选中的单选按钮,您还可以执行以下操作:

$('input:radio:checked').each(function() {
    //this loops through all checked radio buttons
    //you can use the radio button using $(this)
});

回答by Felix Kling

Do you want to process every radio button or do you only need the checked ones? If the latter, it is quite easy:

您是要处理每个单选按钮还是只需要选中的单选按钮?如果是后者,那就很容易了:

$('table input:radio:checked')

Reference: :radio, :checked

参考::radio:checked

回答by PetersenDidIt

var checked = $('#table :radio:checked');

回答by Scott Christopherson

//get the checked radio input, put more specificity in the selector if needed
var $checkedRadio = $("input[type=radio]:checked");

//if you want the value of the checked radio...
var checkedRadioVal = $checkedRadio.val();

回答by Jon

$("table tr input[type=radio]:checked");