2011-11-18 58 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(); 
      } 
     } 
    }); 
});