简体   繁体   English

获取表中的父元素

[英]get parent element in a table

I'm trying to get the parent element in a table. 我正在尝试在表中获取父元素。 I have a checkbox and I want to check the checkbox parent element. 我有一个复选框,我想检查复选框的父元素。

For example 例如

    <td> <a href="./index.html?p=10">$125.55</a> </td>
<td> <input type="checkbox" onChange="getValue(this);" /> </td>

In my getValue function, I would like to get the value 125.55. 在我的getValue函数中,我想获取值125.55。 It will have multiples checkbox in the table inside a loop. 循环内的表中将具有倍数复选框。 Does anyone know how can I get this value? 有谁知道我如何获得这个价值? I can use JQuery or just JS. 我可以使用JQuery或仅使用JS。

Thanks 谢谢

Try this snippet 试试这个片段

function getValue(checkbox){
return checkbox.parentElement.parentElement.cells[0]
     .getElementsByTagName("a")[0].innerHTML;
}

Here you go with the solution https://jsfiddle.net/21vdgoou/1/ 在这里,您可以使用解决方案https://jsfiddle.net/21vdgoou/1/

 getValue = function(e){ console.log($($(e).parent().prev().children()[0]).text()); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tbody> <tr> <td> <a href="./index.html?p=10">$125.55</a> </td> <td> <input type="checkbox" onChange="getValue(this);" /> </td> </tr> <tr> <td> <a href="./index.html?p=10">$195</a> </td> <td> <input type="checkbox" onChange="getValue(this);" /> </td> </tr> <tr> <td> <a href="./index.html?p=10">$155</a> </td> <td> <input type="checkbox" onChange="getValue(this);" /> </td> </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM