JQuery .closest(“tr”)无效

时间:2015-11-30 21:16:00

标签: javascript jquery html

http://jsfiddle.net/yjxq7bae/1/

我试图在输入标签中选择复选框prop。输入标记的id和名称将被重用,因此我必须使用<nobr>文本来缩放dom并获取值。

<tr>
    <td nowrap="true" valign="top" width="190px" class="ms-formlabel">
        <h3 class="ms-standardheader">
        <nobr>Confirmation Sent</nobr>
        </h3>
    </td>
    <td valign="top" class="ms-formbody">
       <span dir="none">
          <input id="ctl00_m_g_ed53ee23_de9d_4105_ba24_00e2a21cef5e_ctl00_ctl05_ctl50_ctl00_ctl00_ctl04_ctl00_ctl00_BooleanField" type="checkbox" name="ctl00$m$g_ed53ee23_de9d_4105_ba24_00e2a21cef5e$ctl00$ctl05$ctl50$ctl00$ctl00$ctl04$ctl00$ctl00$BooleanField" checked="checked" /><br />
       </span>
    </td>
</tr>
<div>Click Here</div>

我尝试过各种可能的方式。如果您从.closest()元素再次上升一次,您会注意到<h3>失败。通过首先更改h3的css,然后尝试隐藏td,小提琴演示了这个。

var mop = $('nobr').filter(function () {
    return $(this).text() === 'Confirmation Sent'
}).closest("tr").find("input[type=checkbox]").prop('checked');
alert(typeof mop);

$('nobr:contains(Confirmation Sent)').closest("h3").css("background", "yellow");
$('nobr:contains(Confirmation Sent)').closest("h3").closest("td").hide();


$('div').on("click ", function (event) {
    var toast = $('nobr').filter(function () {
        return $(this).text() === 'Confirmation Sent'
    }).closest("tr").find("input[type='checkbox']").prop('checked');
    alert(typeof toast);
});

1 个答案:

答案 0 :(得分:13)

问题是您没有正确的表语法。您错过了行周围的<table></table>标记。

由于它不是有效的表格,因此浏览器会抛弃您的<tr><td>代码:

Screenshot of resulting HTML structure in the DOM

如果您将HTML包装在<table>中,则您的javascript按预期工作。

相关问题