$('.btn-delete').click(function(){
var prm_delete = $.ajax({
type: 'POST',
url: '/gateway/delete',
data: {id: $(this).attr('id')}
});
prm_delete.done(function(data){
//how do I get the button?
//$(this) does not work
});
});
上面是我的删除按钮代码,我使用promise发送按钮的id。承诺完成后,我需要按下按钮,这样我才能突出显示它。
我在使用示波器时遇到问题运行功能,如何传递按下的按钮?
答案 0 :(得分:2)
使用ajax的选项上下文:
var prm_delete = $.ajax({
context: this,
type: 'POST',
url: '/gateway/delete',
data: {id: $(this).attr('id')}
});
您也可以绑定上下文:
prm_delete.done(function(data){
//how do I get the button?
//$(this) does not work
}.bind(this));
与jin $.proxy
中的usin相同(为旧浏览器提供支持):
prm_delete.done($.proxy(function(data){
//how do I get the button?
//$(this) does not work
},this));
答案 1 :(得分:1)
尝试将$(this)
存储在另一个变量中,以便稍后访问:
$('.btn-delete').click(function(){
var that = $(this);
var prm_delete = $.ajax({
type: 'POST',
url: '/gateway/delete',
data: {id: $(this).attr('id')}
});
prm_delete.done(function(data){
// Now you can use that to access the `.btn-delete`
});
});