[英]Jquery simple function
此代碼的目的是使用AJAX刪除注釋。 該函數調用如下:
DeleteComment(166);
並且運行的代碼是:
// Each delete button
function DeleteComment(CommentID) {
$.ajax({
url: AJAXURL + "?action=del&id=" + CommentID,
success: function (data) {
// Parse the data
if (data.substring(0, 1) == "1") {
$('#cid' + CommentID).hide();
} else {
alert(data.substring(2, data.length));
}
}
});
}
但是$('#cid' + CommentID).hide();
line永遠不會觸發,因為CommentID
沒有保留,我是Jquery的新手,有人可以告訴我如何更改這個,以便在調用ajax成功時保留注釋ID嗎?
把$('#cid' + CommentID).hide();
在$.ajax({
之前$.ajax({
然后添加$('#cid' + CommentID).show();
到你的其他條件..
首先隱藏它然后如果刪除失敗則重新顯示它...
不是最優雅的解決方案,而是來自你所處位置的阻力最小的路徑。
你能發布更多周圍的代碼嗎? 因此,您的代碼看起來應該可以工作。 但我看到一個麻煩的評論: // Each delete button
。 將DeleteComment函數綁定到按鈕的方式不能像您假設的那樣工作。
試試這個:
// Iterate over each delete button.
// The .each(...) provides you a function, a.k.a. local scope, for each button.
$(".deleteButtons").each(function (idx, el) {
// This is very important: you must create a local variable to hold the ID.
// How to get the ID is up to you.
var id = getTheCorrespondingCommentId(idx, el);
// Now you can safely pass the local variable to the DeleteComment function:
$(el).click(function() { DeleteComment(id); });
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.