2014-09-19 48 views
0

我需要一點幫助。我想補充的jQuery UI自動完成,JQUERY UI .auto-complete

$("#search").autocomplete({ source: $post }); 

下面的代碼,我不知道如何去做好它..

// Communication to PHP file 

function searchq() {  
    var searchTxt = $("input[name='search']").val(); 

    $.post("tagasearch.php", {searchVal: searchTxt}, function(output){ 
     $("#output").html(output); 
    }); 
}; 

他們這樣的代碼,現在的工作是它打印到HTML的形式

<div id="output"></div> 

回答

0

也許你沒有在您的網頁中包含正確的CSS?此外,我不確定只需將$ post作爲源代碼。我通常做這樣的事情:

<input type="text" id="search"> 

<script> 
    jQuery('#search').autocomplete({ 
     source: function(request, response) {  

      jQuery.ajax({ url: toCall, 
      dataType: "json", 
      data: { term: request.term }, 
      success: function(data) { 
       response(data); 
       } 
      }); 

    } //whatever other setup you want for autocomplete like minLength, etc. 
    }); 

</script> 
+0

感謝您的回覆,我只是使用了代碼$(「#search」)。autocomplete({source:$ post});作爲一個例子..我想知道現在在我的PHP腳本中使用什麼源代碼我有「輸出」回聲給我的jQuery。對於CSS我有正確的jQuery UI文件鏈接到我的HTLM頁面。 – TAG 2014-09-19 19:19:57

0

Scott's answer是正確的,但沒有說明太多。我會盡量擴大一點。

首先,請注意,$("#search")id選擇器,而不是name。如果輸入只有一個name=search你可以$("[name=search]")

下一步選擇它,它看起來像你的PHP頁面需要一個叫searchVal參數。如果你把它改寫期待term(這是自動完成的默認request參數),你可以去那麼簡單,通過你的帖子的網址爲source選項:

$("input[name=search]").autocomplete({ source: "tagasearch.php" }); 

第三,如果你使用search: function()期權價值,你可以自定義它或其反應(像斯科特建議):

$("input[name=search]").autocomplete({ 
    source: function(request, response) { 
     $.post("tagasearch.php", { 
      searchVal: request.term 
      //    ^-- request.term is the string you've typed in the input 
     }, response); 
     // ^-- response is the callback passed by jQueryUI 
     // 
     // it expects a single parameter which it treats as the data for the widget 

     // Just using response in the above function is exactly equivalent to: 
     $.post("tagasearch.php", {searchVal: request.term}, 
      function(output) { 
       response(output); 
      } 
    ); 
} 

The documentation有您需要的所有信息;還有a couple of useful examples