Php Jquery实时搜索脚本垃圾邮件
我在live search中编写了以下脚本:Php Jquery实时搜索脚本垃圾邮件,php,javascript,jquery,json,Php,Javascript,Jquery,Json,我在live search中编写了以下脚本: $(document).ready(function(){ $('input#search').keypress(function(){ $('ul#pacientes_hint').html(""); var texto = $('input#search').val(); if(texto.length > 2){ $('ul#pacientes_hint').html("");
$(document).ready(function(){
$('input#search').keypress(function(){
$('ul#pacientes_hint').html("");
var texto = $('input#search').val();
if(texto.length > 2){
$('ul#pacientes_hint').html("");
$.post("../index.php/buscar/busca2", { "texto" : texto},
function(data){
$('ul#pacientes_hint').html("");
var html_final = "";
$.each(data, function(key, value) {
html_final = html_final + value.msg;
});
$('ul#pacientes_hint').html(html_final);
}, "json");
}else{
$('ul#pacientes_hint').html("");
}
});
});
它工作正常,但当我输入太快时,它会发出垃圾邮件查询,我如何才能使它一次只运行一个脚本执行(最后一次按键)?我认为您需要在完成之前终止上一个AJAX请求。试试这个:
$(document).ready(function(){
var xhr;//Declare a global variable
$('input#search').keypress(function(){
$('ul#pacientes_hint').html("");
var texto = $('input#search').val();
if(texto.length > 2){
xhr.abort();//Add this line
$('ul#pacientes_hint').html("");
//store AJAX object in variable
xhr = $.post("../index.php/buscar/busca2", { "texto" : texto},
function(data){
$('ul#pacientes_hint').html("");
var html_final = "";
$.each(data, function(key, value) {
html_final = html_final + value.msg;
});
$('ul#pacientes_hint').html(html_final);
}, "json");
}else{
$('ul#pacientes_hint').html("");
}
});
});
您最好使用Ben Alman的油门/去盎司:
可能重复的抱歉如果是重复的,我只是不知道搜索什么没有问题,有时要找到正确的搜索词可能会很棘手。再次检查您提供的问题,这不是我想要的,有没有办法停止jquery to queue函数并只执行当前函数?