使用 jquery,如何根据变量值检查单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17660168/
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 19:42:01 来源:igfitidea点击:
Using jquery, how do i check a radio button based on a variable value?
提问by Damien
I have a list of radio buttons. I need to make one of them checked based on the variable i have. How do i accomplish this? The problem is i have no control of what the radio button list will look like. So here's my code:
我有一个单选按钮列表。我需要根据我拥有的变量检查其中一个。我如何做到这一点?问题是我无法控制单选按钮列表的外观。所以这是我的代码:
<script>
var preSelect = 'Asian';
</script>
<input type="radio" name="CAT_Custom_378508" id="CAT_Custom_378508_0" value="Alaskan Native" />
Alaskan Native<br />
<input type="radio" name="CAT_Custom_378508" id="CAT_Custom_378508_1" value="Asian" />
Asian<br />
<input type="radio" name="CAT_Custom_378508" id="CAT_Custom_378508_2" value="African American" />
African American<br />
<input type="radio" name="CAT_Custom_378508" id="CAT_Custom_378508_3" value="Caucasian" />
Caucasian<br />
<input type="radio" name="CAT_Custom_378508" id="CAT_Custom_378508_4" value="Hispanic" />
Hispanic<br />
<input type="radio" name="CAT_Custom_378508" id="CAT_Custom_378508_5" value="Native American" />
Native American<br />
<input type="radio" name="CAT_Custom_378508" id="CAT_Custom_378508_6" value="Pacific Islander" />
Pacific Islander<br />
<input type="radio" name="CAT_Custom_378508" id="CAT_Custom_378508_7" value="Other" />
Other
回答by ?????
回答by Danny Tremblay
Try this:
尝试这个:
$('input[type=radio][value=' + preSelect + ']').attr('checked', true);