[英]JQuery / JavaScript to get value from table after clicking on row
Using JQuery or JavaScript, how can I get a value of a <td>
in a <table>
which looks like the following - 使用JQuery或JavaScript,我如何在
<table>
中获得<td>
的值,如下所示 -
<table class="MyTable">
<tr>
<td>John</td>
<td class="MyClass">5</td>
</tr>
<tr>
<td>Sally</td>
<td class="MyClass">9</td>
</tr>
</table>
If a user clicks on "John's row", the value captured should be "5". 如果用户点击“John的行”,则捕获的值应为“5”。 If a user clicks on "Sally's row", the value captured should be "9".
如果用户点击“Sally的行”,则捕获的值应为“9”。
I have the row captured in the following JQuery - 我在以下JQuery中捕获了行 -
$('table.myTable tbody tr').on('click', function () {
//I want to capture <td class="MyClass"> value here, something like this -
var myValue = $("this.td#MyClass").val() //DOESN'T WORK!
}
I'm not sure what my $(this).
我不确定我的
$(this).
statement should look like, I've tried several things like - $("this.td#MyClass").val()
, but it returns 'undefined'. 语句看起来应该是这样的,我尝试了几样的东西,比如 -
$("this.td#MyClass").val()
,但它返回'undefined'。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks!
谢谢!
var myValue = $(this).find('.MyClass').text();
Use .val()
when you're retrieving values from HTML controls, like <select>
elements for instance. 从HTML控件中检索值时使用
.val()
,例如<select>
元素。 Here you're getting text so use .text()
在这里你得到文字所以使用
.text()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.