2013-11-26 29 views
0

我想在頁面加載後顯示#results。但我不知道我應該使用什麼事件。任何幫助?jQuery Events加載

$(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 
+0

只要把代碼中的事件,它的外面內部。 – PSL

回答

1

您可以加載它在文檔準備回調

$(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 
+0

太棒了!工作工作工作 :) – surname