Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问DOM树中的特定字段_Javascript_Jquery_Dom_Checkbox_Each - Fatal编程技术网

Javascript 访问DOM树中的特定字段

Javascript 访问DOM树中的特定字段,javascript,jquery,dom,checkbox,each,Javascript,Jquery,Dom,Checkbox,Each,我有以下html输出: <tr> <td><input type="checkbox"/></td> <td>Value 1</td> </tr> 及 对于这两个示例,我都得到一个空输出。 你知道这里出了什么问题吗?我希望您能解释一下。.parent()是td,您需要找到下一个兄弟姐妹 var checkValues = $('input[type=checkbox]:checked').map(fun

我有以下html输出:

<tr>
  <td><input type="checkbox"/></td>
  <td>Value 1</td>
</tr>

对于这两个示例,我都得到一个空输出。 你知道这里出了什么问题吗?我希望您能解释一下。

.parent()
td
,您需要找到下一个兄弟姐妹

var checkValues = $('input[type=checkbox]:checked').map(function() {
    return $(this).parent().next().text();
}).get();


尝试使用距离最近的
tr
查找第二个
td

$("tr td input[type=checkbox]").each(function( index ) {
    if (this.checked) {
        alert($(this).closest('tr').find('td:eq(1)').text());
    }
});
或者您可以转到
父级
td
并使用
下一步

alert($(this).parent().next().text());

需要注意的是,如果您的DOM结构发生变化,第二种方法将被破坏,而第一种方法对变化更具鲁棒性。

谢谢您,这将起作用!我需要10分钟来接受这个答案。
$("tr td input[type=checkbox]:checked").each(function (index) {
    alert($(this).parent().next().text());
});
$("tr td input[type=checkbox]").each(function( index ) {
    if (this.checked) {
        alert($(this).closest('tr').find('td:eq(1)').text());
    }
});
alert($(this).parent().next().text());