【发布时间】:2017-03-17 11:30:42
【问题描述】:
当我在 jQuery 中使用 .focusout() 函数时,它似乎在我第二次触发事件时触发了两次,这是我的代码示例:
$(document).ready(function() {
var baseUrl = "http://annuityadvicecentre.dev/";
if($('html').hasClass('ver--prize-soft')) {
$('#home_telephone').focusout(function () {
var softResponse = {};
var inputVal = $(this).val();
$.ajaxSetup({
headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
$.ajax({
type: 'POST',
url: baseUrl + "lookup",
data: {
number: inputVal,
phone_type: "mobile",
},
error: function() {
console.log('POST Error: Mobile');
},
}).done(function(data) {
// you may safely use results here
softResponse.isMobile = data;
});
$.ajax({
type: 'POST',
url: baseUrl + "lookup",
data: {
number: inputVal,
phone_type: "landline",
},
error: function() {
console.log('POST Error: Landline');
},
}).done(function(data) {
// you may safely use results here
softResponse.isLandline = data;
});
$(document).ajaxStop(function () {
console.log('All AJAX Requests Have Stopped');
});
});
}
});
抱歉这个混乱的例子,因为我刚刚启动了这个,但是你可以看到我正在包装这个 focusout 函数:
$('#home_telephone').focusout(function () {...
围绕我的 AJAX 调用,现在由于某种原因,当我在页面上测试它并且不关注 #home_telephone 元素时,.ajaxStop() 函数只运行一次,这是我想要的功能但是如果我然后点击元素并再次取消焦点 .ajaxStop() 函数运行两次。知道为什么会发生这种情况吗?谢谢
【问题讨论】:
-
我认为焦点事件在您的页面中注册了两次。
标签: javascript jquery ajax jquery-focusout