我发布了一个类似的问题,它得到了正确的回答,但这并不是我真正想要的(我的错)。
假设我有 HTML:
<TD nowrap="true" valign="top" width="190px" class="ms-formlabel">
<H3 class="ms-standardheader">
<nobr>Islands</nobr>
</H3></TD>
<TD valign="top" class="ms-formbody">
<span dir="none"><table>
<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="...">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="...">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="...">Hawaii</label></span></td>
</tr>
</table></span>
</TD>
<TD nowrap="true" valign="top" width="190px" class="ms-formlabel">
<H3 class="ms-standardheader">
<nobr>Countries</nobr>
</H3></TD>
<TD valign="top" class="ms-formbody">
<span dir="none"><table>
<tr><td><span class="ms-RadioText" title="Australia"><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="...">Australia</label></span></td>
</tr><tr><td><span class="ms-RadioText" title="Greece"><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="...">Greece</label></span></td>
</tr><tr><td><span class="ms-RadioText" title="UK"><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="...">UK</label></span></td>
</tr>
</table></span>
</TD>
根据选择的岛屿,我想隐藏/显示一些其他字段:
$("input[name=ctl00$m$g_f1c8ac33_8a36_42be_8901_5201d5b9eede$ctl00$ctl04$ctl02$ctl00$ctl00$ctl04$ctl00$RadioButtons]:radio").change(function() {
var title = $(this).next().text();
if(title == "Tasmania")
{
$('nobr:contains("Extra column")').closest('tr').hide();
}
else
{
$('nobr:contains("Extra column")').closest('tr').show();
}
});
与其他单选按钮组一起工作,我想根据选择的国家/地区隐藏/显示元素。我宁愿不使用这些长 id,并且组的最接近的唯一标识符是标签之间的内容,即
<nobr>Islands</nobr>
<nobr>Countries</nobr>
我开始尝试将单选按钮组的名称放入变量中,但没有成功
var rb1 = $('nobr:contains("Extra column")').parent().parent().next().children().children().children().attr('id');
alert(rb1);
但是我没有得到弹出窗口(成功执行后的代码),你如何从 Islands 开始获取单选按钮组名称?
提前致谢。
最佳答案
如果你想缩短你的选择器,你可以使用endswith选择器...
$("input[name$=RadioButtons]:radio")
关于javascript - 帮助使用 jQuery 获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7243395/