2011-11-18 97 views
12

我有一個使用AJAX刪除的項目列表。jQuery ajax this.id undefined

此列表是一個簡單的列表,其中divs和div分別作爲id,因此當從數據庫中刪除項目時,我返回true,然後刪除該行。

這裏我的代碼:

HTML

<div id="row1"> 
<div>item1</div> 
<div><a href="...">view</a></div> 
<div><a id="1">delete</a></div> 
</div> 

JS

$('.delete').click(function() { 
    if (!confirm('Are you sure you want to delete?')) { 
     return false; 
    } 
    $.ajax({ 
     type: "POST", 
     url: '/delete_record', 
     data: 'id=' + this.id, 
     cache: false, 
     success: function (result) { 
      if (result == 'good') { 
       $('#row' + this.id).remove(); 
      } 
     } 
    }); 
}); 

出於某種原因,this.id不起作用,因爲this.id是不確定的,爲什麼?我在我的href上有id="1"

+0

有是有類'.delete'沒有元素,如果有您需要保存在單擊處理參考'this',並參考了成功處理程序中。 – Esailija

+0

可能重複[$(this)在AJAX成功不起作用](http://stackoverflow.com/questions/6394812/this-inside-of-ajax-success-not-working) –

回答

14

的這一點,你正在使用是指AJAX對象,因爲有內一個新的作用域該功能。如果你想訪問範圍外的變量,你必須在ajax調用之前聲明它們。

$('.delete').click(function() { 
    if (!confirm('Are you sure you want to delete?')) { 
     return false; 
    } 
    var _this = this; 
    $.ajax({ 
     type: "POST", 
     url: '/delete_record', 
     data: 'id=' + this.id, 
     cache: false, 
     success: function (result) { 
      if (result == 'good') { 
       $('#row' + _this.id).remove(); 
      } 
     } 
    }); 
}); 
0

試圖聲明之前$.ajax$this

,如:

變量$ =這本;

並使用該變量。

$('.delete').click(function() { 
    var $this = this; 

    if (!confirm('Are you sure you want to delete?')) { 
     return false; 
    } 

    $.ajax({ 
     type: "POST", 
     url: '/delete_record', 
     data: 'id=' + $this.id, 
     cache: false, 
     success: function (result) { 
      if (result == 'good') { 
       $('#row' + $yourid).remove(); 
      } 
     } 
}); 
}); 
+0

好點,但你可能不會不需要將'this'變量包裝在一個jQuery對象中,因爲所需要的只是ID。怎麼樣:'var that = this;'因爲你可以在AJAX回調函數中使用'that.id'。 – Jasper

+0

是的,錯過了,更新。謝謝@Jasper –

+0

當你得到一個元素的id時,它會創建'var $ thisId = this.id'的開銷少得多,而不是將'this'包裝到一個jQuery對象中。 – Jasper

-1

我在您的html中看不到任何帶有'delete'類的元素。假設它是刪除鏈接,您需要獲取$(this).attr('id')而不是this.id。

+2

'this.id'返回這個id而不會創建一個jQuery對象的額外開銷。 – Jasper

+0

有你。剛剛醒來=( –

4

您的ID不應以數字開頭。以下是如何制定一個有效的ID:What are valid values for the id attribute in HTML?

this在您的成功回調函數中沒有引用您的點擊處理函數中的this。所以,你需要在你的點擊處理程序this的引用,以便它可以在你的回調函數訪問:

$('.delete').click(function() { 
    if (!confirm('Are you sure you want to delete?')) { 
     return false; 
    } 
    var that = this; 
    $.ajax({ 
     type: "POST", 
     url: '/delete_record', 
     data: 'id=' + this.id, 
     cache: false, 
     success: function (result) { 
      if (result == 'good') { 
       $('#row' + that.id).remove(); 
      } 
     } 
    }); 
}); 
+0

我對所有投票都表示反對,只是想知道如何提高我的知識水平。 – Jasper

0
$('.delete').click(function() { 
    if (!confirm('Are you sure you want to delete?')) { 
     return false; 
    } 

    var id = this.id; // here is the magic 

    $.ajax({ 
     type: "POST", 
     url: '/delete_record', 
     data: 'id=' + this.id, 
     cache: false, 
     success: function (result) { 
      if (result == 'good') { 
       $('#row' + id).remove(); 
      } 
     } 
    }); 
});