将预加载程序添加到此JQuery.post()脚本
我正在使用以下代码获取相关搜索结果:将预加载程序添加到此JQuery.post()脚本,jquery,Jquery,我正在使用以下代码获取相关搜索结果: <form> <input id="message" type="text" name="search" value=""/> </form> <div id="searchresults" class="contentbox"></div> <div id="noresult" class="contentbox" style="display:none">no results
<form>
<input id="message" type="text" name="search" value=""/>
</form>
<div id="searchresults" class="contentbox"></div>
<div id="noresult" class="contentbox" style="display:none">no results found.</div>
function getticketsuggestions() {
currentcheckcontent = $("#message").val();
if (currentcheckcontent) {
$.post("submitticket.php", { action: "getkbarticles", text: currentcheckcontent },
function(data){
if (data) {
$("#searchresults").html(data);
$('#noresult').hide();
$("#searchresults").fadeIn('slow');
}
else {
$('#noresult').fadeIn(3000);
$("#searchresults").hide();
}
});
lastcheckcontent = currentcheckcontent;
}
setTimeout('getticketsuggestions();', 3000);
}
getticketsuggestions();
没有发现任何结果。
函数getticketsuggestions(){
currentcheckcontent=$(“#消息”).val();
if(当前检查内容){
$.post(“submitticket.php”,{action:“getkbarticles”,text:currentcheckcontent},
函数(数据){
如果(数据){
$(“#搜索结果”).html(数据);
$('#noresult').hide();
$(“#搜索结果”).fadeIn('slow');
}
否则{
$(#noresult')。法代因(3000);
$(“#搜索结果”).hide();
}
});
lastcheckcontent=currentcheckcontent;
}
setTimeout('getticketsuggestions();',3000);
}
getticketsuggestions();
这段代码工作得很完美。我在
我想在从submitticket.php获取结果时添加预加载程序“加载…”
我怎样才能做到这一点
谢谢您可以使用
.ajaxStart()
+.ajaxStop()
处理程序,它将为您要发送的所有ajax请求全局启动。因此,您可以在此处显示和隐藏一些DIV
或其他内容
第二个选项是使用underlaying.ajax()
调用,而不是$.post()
。在这里可以覆盖之前发送
和完成
处理程序来做同样的事情,显示和隐藏一些信息
例如:
$.ajax({
url: "submitticket.php",
data: {
action: "getkbarticles",
text: currentcheckcontent
},
type: 'POST',
dataType: 'html',
beforeSend: function(xhr){
$('#info').show(); // #info must be defined somehwere
},
success: function(data){
if (data) {
$("#searchresults").html(data);
$('#noresult').hide();
$("#searchresults").fadeIn('slow');
}
},
complete: function(xhr, textStatus){
$('#info').hide(); // #info must be defined somehwere
}
});
参考:,如果出于某种原因不想使用.ajax(),则可以使用代码执行此操作:
$('#someDiv').html('loading...');
$.post("submitticket.php", { action: "getkbarticles", text: currentcheckcontent },
function(data){
if (data) {
$("#searchresults").html(data);
$('#noresult').hide();
$("#searchresults").fadeIn('slow');
}
else {
$('#noresult').fadeIn(3000);
$("#searchresults").hide();
}
$('#someDiv').html('');
});
@莫因:我已经试过了,但是$('someDiv').html('';由于设置超时而保持显示隐藏