[英]Using AJAX or jQuery to update an HTML table
如果只有一个简单的按钮,则会从表中删除一行,例如:
<tr id="table_row_id">
<td>some info</td>
<td><button class="remove-row">Remove</button></td>
</tr>
是的,您需要jQuery侦听器用于该按钮上的click事件。 例如:
// This could easily be a call to .click()
$('.remove-row').on('click', function() {
var idToRemove = $(this).parent().attr('id');
$.ajax({
method: "POST",
data: {
'action': 'remove',
'id': idToRemove
},
success: function(response) {
// here you can check if the element is removed from the server
// based on the response that the server returns.
// And if it is, you can remove it from the dom.
$('#' + idToRemove).remove();
},
error: function(error) {
// Here you can show an user message, "Sorry cannot remove element."
} //...
});
});
您需要意识到的是,这是异步编程,您只需要定义回调即可。 服务器完成工作并收到响应后,将调用回调。
因此,您可以根据用户交互或时间间隔重新加载表。
我建议:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.