2016-03-28 52 views
0

我有一个load.gif,每次用户进行AJAX强力搜索时启动。但是,我有一些搜索字段会在用户输入时自动显示建议,这些字段也由AJAX提供支持。将ajaxStart函数限制为2个ajax函数中的1个

现在我的loads.gif出现在用户搜索以及搜索建议,而打字。如何限制显示loading.gif的函数仅在用户使用AJAX搜索时显示,而不能在输入AJAX搜索时显示搜索建议?

这是我的函数:

1)添加一个全局变量,如showLoadingAnimation并将其设置为取决于true或false:

$(document).ajaxStart(function() { 
    $(".se-pre-con").fadeIn("fast"); 
}).ajaxStop(function() { 
    $(".se-pre-con").fadeOut("fast"); 
}); 
+0

怎么样将其绑定与条件一样,如果用户是STI如果用户不在搜索输入或搜索输入的第一个联系人,然后显示load.gif –

+0

你是什么意思与“绑定条件,如果用户是仍然在搜索输入“ – mesqueeb

+0

请看我的答案。 –

回答

0

如何将其与条件绑定,如用户仍然在搜索输入然后不显示loading.gif其他如果用户超出搜索输入或搜索输入的第一个接触然后显示loading.gif(下面参考)

第一全局变量

var input_focus = false; 

,然后当指定的输入是焦点

$("#specified_input").focus(function(){ 
    //set the variable named 'input_focus' to true to reject the showing of the loader (loading.gif) or hide it. 
    input_focus = true; 
}).blur(function(){ 
    //when the specified input lose it focus then set the variable 'input_focus' to false so that the loader (loading.gif) is allowed to show 
    input_focus = false; 
}); 

$.ajax({ 
    url : 'my-url', 
    type : 'post', 
    data : {}, 
    beforeSend : function(){ 
     //check if input is on focus 
     if(input_focus !== true){ 
      //show the loading.gif, assume that #loader 
      $("#loader").show(); 
     }else{ 
      //hide the loading.gif, assume that #loader 
      $("#loader").hide(); 
     } 
    }, 
    complete : function(){ 
     //when the ajax request is complete 
    }, 
    success : function(response){ 
     //the response function 
    } 
}); 
0

我想通过以下任一解决它需要。在您ajaxStartajaxStop做到以下几点:除了更改jQuery的全局设置的

$(document).ajaxStart(function() { 
    if (showLoadingAnimation) $(".se-pre-con").fadeIn("fast"); 
}).ajaxStop(function() { 
    if (showLoadingAnimation) $(".se-pre-con").fadeOut("fast"); 
}); 

2),总结了jQuery方法用自己的方法:

//only listen to ajaxStop event so that we can hide the animation 
$(document).ajaxStop(function() { 
    $(".se-pre-con").fadeOut("fast"); 
}); 

function myAjax(params, showAnimation) { 
    if (showAnimation) $(".se-pre-con").fadeIn("fast"); 
    $.ajax(params); 
} 

//in your code you instead of calling $.ajax({...}) simply use `myAjax({...})` 

希望这有助于。

相关问题