$('#progress', this).show();
$('#txt', this).hide();
$.ajax({
type: "POST",
url: "./inviteAdminAddSample.php",
data: {
rev: rev,
aid: aid,
rating: rating
},
success: function(data) {
$('#txt', this).html('Succesfully added!'); // ***
$('#txt', this).show(); // ***
$('#progress', this).hide(); // ***
}
});
这3行没有应用,但是如果我把它们放在成功之外(例如 in request of example ),它就可以了。
这里的交易是什么?
答案 0 :(得分:0)
this
并不是指你想要它的元素。
您可以执行以下操作:
var element = this;
$('#progress', element).show();
$('#txt', element).hide();
$.ajax({
type: "POST",
url: "./inviteAdminAddSample.php",
data: {
rev: rev,
aid: aid,
rating: rating
},
success: function(data) {
$('#txt', element).html('Succesfully added!'); // ***
$('#txt', element).show(); // ***
$('#progress', element).hide(); // ***
}
});
但是,如果您使用id
属性,我不会发现需要将第二个参数传递给$()
函数。如果您的id
值是唯一的 - 就像它们应该是---您应该能够使用:
$('#progress').show();
$('#txt').hide();
$.ajax({
type: "POST",
url: "./inviteAdminAddSample.php",
data: {
rev: rev,
aid: aid,
rating: rating
},
success: function(data) {
$('#txt').html('Succesfully added!'); // ***
$('#txt').show(); // ***
$('#progress').hide(); // ***
}
});
答案 1 :(得分:0)
成功回调中的“this”指的是它自己的“this”而不是你想要的那个。 试试这个:
var self = this;
$('#progress',self ).show();
$('#txt',self ).hide();
$.ajax({
type: "POST",
url: "./inviteAdminAddSample.php",
data: {rev: rev, aid: aid, rating: rating},
success: function(data) {
$('#txt',self ).html('Succesfully added!'); // ***
$('#txt',self ).show(); // ***
$('#progress',self ).hide(); // ***
}
});
这样,您首先要保存对要定位的“this”的引用。您可以在代码中使用该引用,它总是引用您的意图。