2014-09-19 3 views
0

약간의 도움이 필요합니다.JQUERY UI. 자동 완성

// Communication to PHP file 

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

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

그들은 방법 코드가 지금 작동은 .. 나는 아래의 코드에

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

, jQuery를 UI 자동 완성을 추가하려고 나는 그 일에 대해 이동하는 방법을 잘하지 오전

<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}); 예를 들어 .. 나는 내 jquery에 "출력"에코가있는 PHP 스크립트에서 소스를 사용하려면 무엇을 사용해야하는지 궁금합니다. CSS의 경우 HTLM 페이지에 연결된 올바른 jquery UI 파일이 있습니다. – TAG

0

Scott's answer는 정확하지만 많은 설명하지 않습니다 : 난 보통 이런 일을한다. 나는 그것을 조금 확장하려고 노력할 것이다.

먼저 $("#search")id 선택자이고 name이 아니라는 점에 유의하십시오. 입력 한 내용이 name=search 인 경우에만 선택할 수 있습니다. $("[name=search]")

다음은 PHP 페이지에서 searchVal이라는 매개 변수가 필요합니다. 사용자 정의 할 수는 search: function() 옵션 값을 사용하는 경우,

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

셋째, 당신이 (자동 완성의 기본 request 매개 변수 인) term를 기대를 재 작성하는 경우에는 source 옵션 게시물의 URL을 전달처럼 간단 갈 수 있습니다 그것은 나 (스콧이 시사하는 것처럼)의 응답 :

$("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는 당신이 필요로하는 모든 정보를 가지고; there 's also a couple of useful examples