使用jQuery,我如何基于变量值检查单选按钮? [英] Using jquery, how do i check a radio button based on a variable value?

查看:34
本文介绍了使用jQuery,我如何基于变量值检查单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单选按钮列表.我需要根据我拥有的变量对其中之一进行检查.我该如何做到这一点?问题是我无法控制单选按钮列表的外观.所以这是我的代码:

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

推荐答案

,您可以使用属性等于选择器定位到正确的无线电,然后使用 .prop()设置选中的属性

you can use the attribute equals selector to target the correct radio then use .prop() to set the checked property

$('input[name=CAT_Custom_378508][value=' + preSelect + ']').prop('checked',true)

FIDDLE

这篇关于使用jQuery,我如何基于变量值检查单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆