【发布时间】:2016-04-20 04:26:50
【问题描述】:
我通过 ajax 发送请求以在数据库中插入数据。成功提交后我的按钮消息没有改变。在按下我触发消息Please wait... 时触发成功设置新的html 值Done。 db 中的记录已成功创建,但 Done 的按钮文本未更改。
我的脚本:
var Friend = {
// Add new friend
add: function() {
var btn = $(".btn-add-friend");
btn.click(function() {
$(this).html("Please wait...");
$.ajax({
type: "post",
url: baseurl + "/FriendRequest/send",
data: {
friend: $(this).data('friend')
},
success: function(xhr, status) {
$(this).html("Done"); // Not wortk
alert("done"); // <-- Work
},
error: function(response, s, e) {
alert(response.responseText);
},
complete: function () {
//. the some from success
}
});
});
},
// Initialise
init: function() {
Friend.add();
}
};
HTML:
<button type="button" id="item-<?=$person->account_id;?>" class="btn btn-xs btn-add-friend has-spinner" data-friend="<?= $person->account_id;?>"><i class="fa fa-plus-circle"></i> Add Friend</button>
当我单击按钮时,文本已更改为 Please wait..,但成功后未更改为 Done,并且警报成功执行。
点击后看起来它不是 DOM 的一部分!我也尝试使用on() 得到一些结果。
【问题讨论】:
-
在将变量
baseurl传递给url之前检查它是否具有某些值。 -
baseurl 不是问题,它只是控制器的url,点击所有数据成功插入数据库后。
标签: javascript jquery html ajax dom