我想添加一個鏈接來刪除使用jquery和ajax的mysql數據庫中的一行。數據當前顯示在一個表格中。出於某種原因,Click事件不會觸發。單擊鏈接到Ajax調用的事件處理程序
這裏是我的AJAX調用:
<script>
$(document).ready(function() {
/* load table with page load*/
$("#sort tbody").load("inc/index_table.php");
/* row deletion */
$(".deletelink").click(function(){
var id = $(this).attr("id");
$.ajax({
beforeSend: function (request) {
var answer = confirm("Are you SURE you want to delete this?/nThis action is NOT reversible.")
if (answer){ return(true); }
else { return(false); }
},
type: "POST",
url: location.href,
data: "delete="+id,
error: function() {
console.log("Theres an error with AJAX deletion");
},
success: function(){ //a.td.tr.remove just that row rather than the whole table
$this.parent().parent().remove();
console.log("Deleted.");
}
});
});
});
</script>
和相關的HTML: 這是一個while循環,從我的數據庫打印表的一部分:
<td><a class="deletelink" id="'.$row["id"].'"><img src="images/delete.png" alt="Delete"></a></td>';
我的代碼指定<a class="deletelink">
但它沒有註冊$(".deletelink").click(function(){ });
有沒有人看到w帽子在這裏可能是錯的,或者有其他方法建議?
錨需要「HREF」屬性是有效的.. – Nicholas