我正在使用此代码初始化datatable并在单击Delete链接后 - 一切正常,但我使用刷新页面,所以我尝试直接使用DataTable函数删除行,但是根本无法使其工作。 ...这是当前代码(有效的代码,但刷新整个页面):
<script type="text/javascript">
$(document).ready(function() {
$('#publishers').dataTable( {
"iDisplayLength": 50,
"sPaginationType": "full_numbers",
"bStateSave": true
} );
} );
function DeletePublisher(publisherid) {
jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher', function(r) { if (r)
$.ajax({
type: "GET",
url: 'includes/publishers/delete-publisher.php?publisherid=' + publisherid,
data: '',
success: function(response){
$.jGrowl('Publisher deleted');
window.location.reload();
}
});
});
}
</script>
并且在身体......
...A LOT OF UNINTERESTING COLUMNS, AND THE ONE WITH ACTION:
<td class="action-th">
<ul class="button-table-head">
<li><div class="button-head edit-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Edit" data-style-tooltip="tooltip-mini-slick"><span>Edit</span></a></div></li>
<li><div class="button-head delete-icon"><a href="#" class="sweet-tooltip" data-text-tooltip="Delete" data-style-tooltip="tooltip-mini-slick" onclick="DeletePublisher('<?php echo $publisher_id; ?>')"><span>Delete</span></a></div></li>
</ul>
</td>
我尝试使用此代码,但它不起作用:
function DeletePublisher(publisherid) {
jConfirm('Are you sure you want to delete this publisher?', 'Delete publisher',function(r) { if (r)
$('#publishers tbody').on( 'click', 'tr', function () {
var tr = this;
$.ajax({
type: "POST", //or GET
url: 'includes/publishers/delete-publisher.php?publisherid=' + publisherid,
data: '',
success: function(response){
t.fnDeleteRow( tr );
$.jGrowl('Publisher deleted');
}
} );
});
});
}
任何想法为什么 - 我完全不喜欢JS,JQuery ......
答案 0 :(得分:5)
如果您传递对被点击元素的引用,您可以轻松地执行此操作:
onclick="DeletePublisher(this,'<?php echo $publisher_id; ?>')"
然后在你的函数中:
function DeletePublisher(element,publisherid) {
...
success: function(response){
$(element).parents('tr').remove()
$.jGrowl('Publisher deleted');
}
...
}
...然后根本不要致电window.location.reload();