我有一个表,它在它的行的点击事件上运行一个函数。它工作得非常好,但是当通过 AJAX 将 tr 添加到该表时,我无法触发该行的点击事件,或者我们可以说点击事件没有触发该行,除了其他行仍然能够触发点击事件.
$('div.dashboard-content table.list tbody tr').click(function(){
var id = $(this).attr('id');
$('div.dashboard-content table.list tr td').css({'background': '#ffffff','font-weight':'normal'});
$('div.dashboard-content table.list thead td').css({'background': '#EFEFEF','font-weight':'bold'});
$('tr[id='+ id + '] td').css({'background': 'lightblue','font-weight':'bolder'});
$.get(
'index.php?route=sale/order/infoforhome&token=<?php echo $token; ?>',
{ 'order_id' : id },
function(data){
if(data)
{
$('#oder_detail').html
('<table>' +
'<tbody>' +
'<tr>'+
'<td style="font-weight:bold;">' +
'<?php echo $column_orderid; ?>:'+
'</td>'+
'<td>' +
data['orderid'] +
'</td>'+
'</tr>' +
'<tr>'+
'<td style="font-weight:bold;">'+
'<?php echo $column_customername; ?>:'+
'</td>'+
'<td>'+
data['name'] +
'</td>'+
'</tr>'+
'<tr>'+
'<td style="font-weight:bold;">'+
'<?php echo $column_shippingaddress; ?>: '+
'</td>'+
'<td>'+
data['address'] +
'</td>'+
'</tr>'+
'<tr>'+
'<td style="font-weight:bold;">'+
'<?php echo $column_telephone; ?>:'+
'</td>'+
'<td>'+
data['telephone'] +
'</td>'+
'</tr>'+
'<tr>'+
'<td style="font-weight:bold;">'+
'<?php echo $column_orderstatus; ?>:'+
'</td>'+
'<td>'+
data['orderstatus'] +
'</td>'+
'</tr>'+
'<tr>'+
'<td style="font-weight:bold;">'+
'<?php echo $column_ordertotal; ?>:'+
'</td>'+
'<td>'+
data['total'] +
'</td>'+
'</tr>'+
'</tbody>'+
'</table>');
}
else
{
$('#order_detail').html('Sorry No Details exists for this order in the database');
}
}, 'json');
});
function getLatestOrders() {
var last_order_id = $('div.dashboard-content table.list tbody tr:first').attr('id');
$.get(
"index.php?route=common/home/latest&token=<?php echo $token; ?>",
{'order_id' : last_order_id },
function (data) {
if (data) {
for (var i = 0; i < data.length; i++) {
$('div.dashboard-content table.list tbody tr:first').before(
'<tr id="' +
data[i]['order_id'] +
'"><td class="right">' +
data[i]['order_id'] +
'</td><td class="left">' +
data[i]['customer'] +
'</td><td class="left">' +
data[i]['status'] +
'</td><td class="left">' +
data[i]['date_added'] +
'</td><td class="right">' +
data[i]['total'] +
'</td><td class="right"> [<a href="' +
data[i]['action']['href'] + '">' +
data[i]['action']['text'] +
'</a>]</td></tr>'
);
}
}
}
}