1 回答

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊
$.post
是異步方法,它無(wú)法返回任何內(nèi)容。而且您的問(wèn)題是在ajax調(diào)用結(jié)束之前設(shè)置了結(jié)果值。這就是為什么console.log值設(shè)置為的原因0
。
因此,您需要在成功函數(shù)的回調(diào)中進(jìn)行設(shè)置。
并且在回調(diào)
this
元素內(nèi)部為無(wú)效元素,因此您需要在傳遞給成功函數(shù)之前聲明另一個(gè)變量,例如var that = this
代碼:
(function($) {
$.fn.NotYet = function(arti_id) {
this.append(function() {
var that = this;
var result = 0;
$.post("comment_load.php", {
arti_id: arti_id,
status: 1
}, function(data) {
console.log(data); // Returns data from POSTed PHP file
$(that).append(data); //its work
result = data;
});
console.log(result); // Returns 0 //its excute before ajax call
return result; // its not possible
});
};
})(jQuery);
- 1 回答
- 0 關(guān)注
- 137 瀏覽
添加回答
舉報(bào)