[英].focusout() fires twice on second event jQuery
When I am using the .focusout()
function in jQuery it seems to fire twice when I trigger the event for the second time, here is an example of my code: 当我在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');
});
});
}
});
Sorry for the messy example as I have just been bootstrapping this up however you can see I am wrapping this focusout function: 对不起,这个凌乱的示例非常糟糕,因为我刚刚对此进行了自举,但是您可以看到我正在包装此focusout函数:
$('#home_telephone').focusout(function () {...
Around my AJAX calls, now for some reason when I test this out on the page and un-focus on the #home_telephone
element the .ajaxStop()
function only runs once which is the functionality I want however if I then click on the element and un-focus again the .ajaxStop()
function runs twice. 在我的AJAX调用周围,由于某种原因,当我在页面上对此进行测试并且不关注#home_telephone
元素时, .ajaxStop()
函数仅运行一次,这是我想要的功能,但是如果我随后单击该元素并再次取消聚焦.ajaxStop()
函数运行两次。 Any idea why this might be happening? 知道为什么会这样吗? Thanks 谢谢
Try to add e.stoppropogation() within function like: 尝试在函数中添加e.stoppropogation(),例如:
$('#home_telephone').focusout(function (e) {
e.stopPropagation()();
//your code
});
You're adding a new ajaxStop
listener every time the element is unfocused. 每当元素不集中时,您都将添加一个新的ajaxStop
侦听器。 Just move the: 只需移动:
$(document).ajaxStop(function () {
console.log('All AJAX Requests Have Stopped');
});
call outside of the focusout
callback function. 在focusout
回调函数外部调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.