jQuery事件加载

jQuery Events loads

本文关键字:加载 事件 jQuery      更新时间:2023-10-03

我想在页面加载后显示#results。但我不知道我应该使用什么事件。有什么帮助吗?

$(document).ready(function(){
  //Live search
  $("#search").on('keyup' ,function() {
    //Input field value
    var query = $(this).val();
    $.post('search.php', { search: query}, function(cities) {                       
      $('#results').stop(true,true).fadeIn(200).html(cities);  
    });//End ajax call
  });//End on function
});//End document.ready state

您可以将其加载到文档就绪回调中

$(document).ready(function () {
    //this method loads the results based on the passed query
    function load(query) {
        $.post('search.php', {
            search: query
        }, function (cities) {
            $('#results').stop(true, true).fadeIn(200).html(cities);
        }); //End ajax call
    }
    //Live search
    var $search = $("#search").on('keyup', function () {
        //Input field value
        load($(this).val());
    }); //End on function
    //on page load call the load method with the value in seach field
    load($search.val());
}); //End document.ready state