帮助使用jQuery获取元素

时间:2011-08-30 12:50:04

标签: javascript jquery

我发布了一个类似的问题并得到了正确答案,但事实并非如此(我的不好)。

假设我有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);

但我没有得到弹出窗口(执行成功后的代码),你如何通过在群岛开始获得单选按钮组名称?

提前致谢。

2 个答案:

答案 0 :(得分:1)

如果您想缩短选择器,可以使用endswith选择器......

$("input[name$=RadioButtons]:radio")

答案 1 :(得分:0)

$("nobr").closest("td").next().find("input:radio")应该这样做