[英]how to get the value of tr of a table using jquery on click
I have an example table 我有一个示例表
<table class="table">
<tr value="1" class="side-link">
<td>one</td>
<td>two</td>
</tr>
<tr value="2" class="side-link">
<td>one</td>
<td>two</td>
</tr>
</table>
I want to get the value of the selected tr on click function. 我想在点击功能上获得所选tr的值。 what I've tried so far
我到目前为止所尝试过的
$(".table").on('click','.side-link',function(e){
e.preventDefault();
var id = $(this).attr('value');
alert(id);
});
the problem is I cannot address this to the respective "tr" I click , instead it gets the value of .table
. 问题是我无法解决这个问题,我点击了相应的“tr”,而是获得了
.table
的值。 Can anyone solve the issue here? 任何人都可以解决这个问题吗? I would be really grateful!
我真的很感激! THanks :)
谢谢 :)
Note: it needs ( on click ) because I've to replace tr using ajax and the new elements wont be recognized by just using click function !! 注意:它需要(点击),因为我要使用ajax替换tr,并且只使用点击功能不会识别新元素!
Jquery version 1.9.1 Jquery版本1.9.1
You're (as you say) getting the value of the table, not the TR 你(正如你所说)得到了表的价值,而不是TR
Amend your code like this: 像这样修改你的代码:
$(".table tr").on('click','.side-link',function(e){
e.preventDefault();
var id = $(this).attr('value');
alert(id);
});
That should get the correct value. 这应该得到正确的价值。
Your code also working see Demo , check that you have included jquery version >= 1.7
or try to write
your code in $(function(){...})
it may run
before your table renders
您的代码也可以看到Demo ,检查您是否包含了
jquery version >= 1.7
或尝试在$(function(){...})
write
代码$(function(){...})
它可能在您的table renders
之前run
You can also try it like, 你也可以尝试一下,
$(function(){
$(".table .side-link").on('click',function(e){
e.preventDefault();
var id = $(this).attr('value');
alert(id);
});
});
Also if it is id
then why you used value
use id="1" and so on..
此外,如果它是
id
那么为什么你使用value
使用id="1" and so on..
Updated if above not works then try this which never fails(IMHO) if your html
is valid
, 更新如果以上不起作用然后尝试这永远不会失败(恕我直言),如果您的
html
valid
,
$(function(){
$(document).on('click',".table .side-link",function(e){
e.preventDefault();
var id = $(this).attr('value');
alert(id);
});
});
well use HTML5 data attribute.. this is the exact reason, why data attribute was introduce in HTML5. 很好地使用HTML5数据属性..这就是为什么数据属性在HTML5中引入的确切原因。 And make sure you are using jquery's latest version (> 1.6)
.on
was introduced only after jquery 1.6 并确保您使用的是jquery的最新版本(> 1.6)
.on
仅在jquery 1.6之后引入
HTML HTML
<table class="table">
<tr data-value="1" class="side-link">
<td>one</td>
<td>two</td>
</tr>
<tr data-value="2" class="side-link">
<td>one</td>
<td>two</td>
</tr>
</table>
jQuery jQuery的
$(".table").on('click','.side-link',function(e){
e.preventDefault();
var id = $(this).data('value');
alert(id);
});
Okay, look this for get Tr value or Td value: 好的,看看这个获得Tr值或Td值:
HTML Code: HTML代码:
<table class="table" border='1'>
<tr value="1" class="side-link">
<td>one</td>
<td>two</td>
<td>three</td>
</tr>
<tr value="2" class="side-link">
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
jQuery Code: jQuery代码:
$(".table").on('click', 'tr', function () {
var trValue = $(this).attr('value');
var tdValue = $(this).children('td').map(function (index, val) {
return $(this).text();
}).toArray();
// all td value with comma seprated
alert(tdValue);
// current tr attr value
alert(trValue);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.