2014-09-19 50 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