如何使用jquery在<span>中包含单选按钮?</span>

时间:2011-08-30 11:28:52

标签: javascript jquery

我正在使用SharePoint网站,但我无法更改HTML。我想要做的是获取哪个单选按钮,但它们包含在span中。如果我按照跨度上的标题然后按.next()我会在跨度之后得到下一个元素,这是不正确的,所以我应该怎么做?

HTML:

<span dir="none"><table cellpadding="0" cellspacing="1">
<tr>
<td><span class="ms-RadioText" title="Tasmania"><input id="ctl00_m_g_f1c8ac33_8a36_42be_8901_5201d5b9eede_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00" type="radio" name="ctl00$m$g_f1c8ac33_8a36_42be_8901_5201d5b9eede$ctl00$ctl04$ctl02$ctl00$ctl00$ctl04$ctl00$RadioButtons" value="ctl00" /><label for="ctl00_m_g_f1c8ac33_8a36_42be_8901_5201d5b9eede_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl00">Tasmania</label></span></td>
</tr><tr>
<td><span class="ms-RadioText" title="Greenland"><input id="ctl00_m_g_f1c8ac33_8a36_42be_8901_5201d5b9eede_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl01" type="radio" name="ctl00$m$g_f1c8ac33_8a36_42be_8901_5201d5b9eede$ctl00$ctl04$ctl02$ctl00$ctl00$ctl04$ctl00$RadioButtons" value="ctl01" /><label for="ctl00_m_g_f1c8ac33_8a36_42be_8901_5201d5b9eede_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl01">Greenland</label></span></td>
                    </tr><tr>
<td><span class="ms-RadioText" title="Hawaii"><input id="ctl00_m_g_f1c8ac33_8a36_42be_8901_5201d5b9eede_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl02" type="radio" name="ctl00$m$g_f1c8ac33_8a36_42be_8901_5201d5b9eede$ctl00$ctl04$ctl02$ctl00$ctl00$ctl04$ctl00$RadioButtons" value="ctl02" /><label for="ctl00_m_g_f1c8ac33_8a36_42be_8901_5201d5b9eede_ctl00_ctl04_ctl02_ctl00_ctl00_ctl04_ctl00_ctl02">Hawaii</label></span></td>
</tr>
</table></span>

我试图不使用单选按钮组的名称,因为这可能会改变。

提前致谢。

5 个答案:

答案 0 :(得分:1)

$('span > input:radio:checked')

这将选择所有input元素,这些元素为checked,并且是span元素的直接子元素。

Demo

答案 1 :(得分:1)

如果您想要所选的单选按钮:

 $(".ms-RadioText input:radio:checked")

答案 2 :(得分:0)

$(".ms-RadioText > input[type='radio']:checked").val();

答案 3 :(得分:0)

$("span.ms-RadioText input[type='radio']:checked").value('this one is selected');

答案 4 :(得分:0)

就像这样:

$("span.ms-RadioText input[type=radio]:checked") // returns list of all inputs that are children of spans.