jQuery:Looping all radio buttons inside an HTML table
I have an HTML table having n rows and each rows contain one radiobutton in the Row.Using jQuery , How can i look开发者_Go百科 thru these radio buttons to check which one is checked ?
$('#table tbody tr input[type=radio]').each(function(){
alert($(this).attr('checked'));
});
HTH.
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)
});
There are many ways to do that, e.g., using .each
and the .is
traversal method:
$("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
}
});
$('.my-radio-class:checked')
http://api.jquery.com/checked-selector/
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
var checked = $('#table :radio:checked');
$("table tr input[type=radio]:checked");
//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();
精彩评论