使用j查询查找下一个td元素

Finding the next td element using j query?

本文关键字:td 元素 下一个 查找 查询 使用      更新时间:2023-09-26

如何使用jquery找到下一个td元素。我试图将文本或div附加到特定tr中的td元素。我的tr是一个服务器端控件,标签runat=server这是我的查询,它不工作

   var cssdisable= function testcss(rowid) {
    if ($('#testcss').width() != 3) {
        var id = $(rowid).find('td');
        id.append('The following is not valid');
    }
}

这就是我在点击事件

中调用函数的方式
  cssdisable('<%=tr_myrow.ClientID %>');

它不做任何事情,也不给出任何错误。我试图在行id旁边的td元素后添加文本。

有什么想法吗

是HTML有一个名为tr_myrow的行

        <table id="tblstudysub_animal" class="bond_qbs_table" width="100%">
                <tr>
                    <td valign="top">
                        <h3>
                            Select the study subject.<span class="red">*</span></h3>
                        <fieldset>
                            <legend class="hide">Select the study subject.</legend>
                            <table id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_studysubj" border="0">
                        <tr>
                            <td><input id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_studysubj_0" type="radio" name="ctl00$m$g_c09fd465_0ae4_479b_8fc6_21a7de645003$ctl00$rdb_studysubj" value="Humans" /><label for="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_studysubj_0">Humans</label></td>
                        </tr><tr>
                            <td><input id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_studysubj_1" type="radio" name="ctl00$m$g_c09fd465_0ae4_479b_8fc6_21a7de645003$ctl00$rdb_studysubj" value="Non-Human primates" /><label for="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_studysubj_1">Non-Human primates</label></td>
                        </tr><tr>
                            <td><input id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_studysubj_2" type="radio" name="ctl00$m$g_c09fd465_0ae4_479b_8fc6_21a7de645003$ctl00$rdb_studysubj" value="Rodents" /><label for="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_studysubj_2">Rodents</label></td>
                        </tr><tr>
                            <td><input id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_studysubj_3" type="radio" name="ctl00$m$g_c09fd465_0ae4_479b_8fc6_21a7de645003$ctl00$rdb_studysubj" value="Others" /><label for="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_studysubj_3">Others</label></td>
                        </tr>
                    </table>
                        </fieldset>
                    </td>
                </tr>
                <tr id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_tr_myrow">
                        <td valign="top">
                        <div id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_studysub_popul">
                            <h3>
                                Select your study subject.<span class="red">*</span></h3>
                            <fieldset>
                                <legend class="hide">Select your study subject.</legend>
                                <table id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_study_popul" border="0">
                            <tr>
                                <td><input id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_study_popul_0" type="radio" name="ctl00$m$g_c09fd465_0ae4_479b_8fc6_21a7de645003$ctl00$rdb_study_popul" value="Individuals" /><label for="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_study_popul_0">Individuals</label></td>
                            </tr><tr>
                                <td><input id="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_study_popul_1" type="radio" name="ctl00$m$g_c09fd465_0ae4_479b_8fc6_21a7de645003$ctl00$rdb_study_popul" value="Population" /><label for="ctl00_m_g_c09fd465_0ae4_479b_8fc6_21a7de645003_ctl00_rdb_study_popul_1">Population</label></td>
                            </tr>
                        </table>

       <div id="testcss">
    </div>

我正在尝试将该文本添加到该行的td…

一个问题是,您正试图通过其id查询td,但没有在选择器开始时使用# .

try cssdisable('#<%=tr_myrow.ClientID %>');

还为了坚持trtd子元素使用.children()而不是.find('td') (将找到所有后代td元素)

var cssdisable = function testcss(rowid) {
    if ($('#testcss').width() != 3) {
        var id = $(rowid).children().first();
        id.append('The following is not valid');
    }
}

在http://jsfiddle.net/D4z9b/(使用您的示例HTML)演示

在表构建阶段为<td>..</td>元素提供有意义的id(如果不构建它,只需在使用单独调用开始任何处理之前分配它们),然后生成id。像tableName + '_' + row + '_'这样的东西可能会很好。