我正在使用 jquery_ujs 发送一个简单的发布请求。我将回调函数绑定(bind)到“ajax:success”事件。但是,对于每个成功的帖子,此回调都会被调用两次。
我知道这与此类似 other question ,但我检查了我的源代码,它似乎没有加载 jquery_ujs.js 文件两次(正如许多回复所建议的那样)。
$(".new_item").bind("ajax:success",
function(xhr, data, status)
{
/* this is called twice for each call!*/
}
有人指点一下吗?谢谢!
最佳答案
正在发生一些事情之一。
您有多个具有 .new_item 类的元素
alert($(".new_item").length);
您发送了两次ajax请求
alert("Sending Request");
$.ajax(...
或者您绑定(bind)事件两次
alert("Binding Event");
$(".new_item").bind("ajax:success", function(xhr, data, status) {
注意:如果您使用的是现代浏览器,请将警报替换为 console.log()
关于jquery - "ajax:success"被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13017890/