I have the following HTML:
<input type="radio" value="213490" id="lt_1133041_213490" name="lt_1133041[]">
<label for="lt_1133041_213490">LH License</label>
<input type="radio" value="213491" id="lt_1133041_213491" name="lt_1133041[]">
<label for="lt_1133041_213491">PC License</label>
What I need to do is to be able to check the radio based on the label text, so if I pass "LH License" it will check id lt_1133041_213490, if I pass "PC License", it will check id lt_1133041_213491.
Not sure exactly how to do that.
id = $("label:contains("+label_text+")").attr('for');
$('#' + id).prop('checked', true);
Are you looking for something like this? See fiddle .
function checkRadio(text){
$('#'+$('label:contains('+text+')').attr('for')).attr('checked', 'checked');
}
checkRadio('LH License');
This creates a function that when passed a string will check the appropriate radio button.
function checkRadio( text){
$('input:radio').filter(function(){
return $(this).next('label').text()==text;
}).prop('checked', true);\
}
To use function:
checkRadio( 'LH License');
This works perfectly.
var _radio = $("label:contains('LH License')").attr("for");
i = $("#" + _radio).attr('id');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.