Jquery 获取已单击表行的属性
我的表格如下:Jquery 获取已单击表行的属性,jquery,html,Jquery,Html,我的表格如下: <table class="table table-bordered table-hover" style="background: white; margin-top: 5px;"> <thead> <tr> <th style="width: 5%">#</th> <th>Group</th> <
<table class="table table-bordered table-hover" style="background: white; margin-top: 5px;">
<thead>
<tr>
<th style="width: 5%">#</th>
<th>Group</th>
<th style="width: 30%">Sub Group</th>
</tr>
</thead>
<tbody id="allAccessInfo" name="allAccessRow">
<tr class="test" access_id="1">
<th scope="row"><i class="fa fa-file-photo-o"></i></th>
<td id="name_file_edit_undefined">Custom Order</td>
<td id="name_file_edit_undefined">Art Tab</td>
</tr>
<tr class="test" access_id="2">
<th scope="row"><i class="fa fa-file-photo-o"></i></th>
<td id="name_file_edit_undefined">Custom Order</td>
<td id="name_file_edit_undefined">Credit Tab</td>
</tr>
<tr class="test" access_id="3">
<th scope="row"><i class="fa fa-file-photo-o"></i></th>
<td id="name_file_edit_undefined">Custom Order</td>
<td id="name_file_edit_undefined">Imprint Tab</td>
</tr>
<tr class="test" access_id="4">
<th scope="row"><i class="fa fa-file-photo-o"></i></th>
<td id="name_file_edit_undefined">Custom Order</td>
<td id="name_file_edit_undefined">Information Tab</td>
</tr>
</tbody>
</table>
我怎样才能得到它?因为,我使用ajax获取表行,所以无法将其与带有As
$('tr.test')的表行绑定。单击(函数(事件)
因为在加载javascript时,该行首先不存在。因此,我已为单击事件绑定了
。假设您使用AJAX将内容附加到tbody。请在将这些行附加到代码后将事件单击绑定到tr:
$('#allAccessInfo').find('tr')
.unbind('click')
.bind('click', function(event)
{
alert($(this).attr('access_id'));
}
);
我们发现tr(不是tbody)问题在于
这个在您的代码上下文中不是tr
,而是tbody
。tbody
没有访问id
属性,因此是“未定义”的。您可以如下更改它:
$('tbody[name="allAccessRow"]').on('click', 'tr.test', function(event)
{
alert($(this).attr('access_id'));
}
);
这将解决您的问题。在获取每一行时,您可以在成功处理程序中绑定click事件
$.ajax(config).done(function(response){
$('TR',response).each(function(i,tr){
$(tr).bind('click', this.someCommonCallback );
$('tbody').append( tr );
});
});
其中,someCommonCallback
返回您从行中查找的属性。OP声明他使用ajax
获取表行。由于这些行是动态创建的,他无法直接绑定到表行上的单击事件。这就是为什么他绑定到tbody
。
$.ajax(config).done(function(response){
$('TR',response).each(function(i,tr){
$(tr).bind('click', this.someCommonCallback );
$('tbody').append( tr );
});
});