当我.focusout()
在 jQuery 中使用该函数时,它似乎在我第二次触发事件时触发了两次,这是我的代码示例:
$(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()
功能运行两次。知道为什么会发生这种情况吗?谢谢