ajax live search,Live Search With jquery AJAX?

许博达
2023-12-01

How can I create a live search with Jquery AJAX, I used keypress or keyup event to loop query, but if I type 3 characters or more the AJAX will do 3 times or more.

My form:

Here my AJAX:

jQuery(document).ready(function(){

(function($){

$(".keyword-search").keypress(function(){

var keyword = $(this).val();

$(".search-appear").empty();

$.ajax({

type: "post",

url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",

data: { action: 'get_tour', keyword: keyword },

beforeSend: function() {$("#loading").fadeIn('slow');},

success: function(data) {

$("#loading").fadeOut('slow');

$(".search-appear").append(data);

}

});

});

})(jQuery);

});

And Here is my demo function:

function get_tour()

echo 'Do something!';

?>

And this is result when I type 3 characters:

type 3 characters

I type 2 chars:

type 2 characters

Who can help me it work 1 time no matter how many key press. Or it work fine anyway ! Thanks a lot !!

 类似资料:

相关阅读

相关文章

相关问答