简体   繁体   English

根据标签文字检查收音机

[英]Check Radio based on label text

I have the following HTML: 我有以下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. 我需要做的是能够根据标签文本检查无线电,所以如果我通过“ LH许可证”,它将检查ID lt_1133041_213490,如果我通过“ PC许可证”,它将检查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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM