我有一个动态加载的按钮,如下所示:
<button type="button" class="btn btn-default admire"><span class="font awesome"> 3</span></button>
我尝试清空文本值(在本例中为 3),并使用单击按钮时 AJAX 调用返回的值来更新它。这是到目前为止我的代码:
$(document.body).on("click", "button.admire", function(){
$.post('ajax/example.php', function(data){
$(this).find("span").empty();
$(this).find("span").append(data);
});
});
它没有按预期工作。 “this”按钮在 ajax 函数中仍然可以引用吗?还是超出范围了?
编辑:我如何在ajax调用函数中引用这个范围?
谢谢
威廉
最佳答案
如果我明白你的意思,请尝试这样做,并且不要忘记向 btn 添加类
$(document.body).on("click", "button.admire", function(){
var btn = $(this);
$.ajax({
url : "your link",
type: "POST",
dataType: "Json",//by default it's returning html
data: "your data",// if you sending data
async: "false", // it is mean, that until ajax request is not completed it not going forword
complete: function (response) {
btn.find("span").empty();
btn.find("span").append(response.yourresponse);
}
})
})
关于jquery - 使用 JQuery 清空动态加载的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25474659/