2012-06-17 10 views
1

데이터베이스의 레코드를 검색 할 수있는 검색 창을 만들고 싶습니다.항목에 대한 Jquery UI 자동 완성 링크

search.php은 다음과 같습니다

ReferenceError: item is not defined 

나는 일이 원하는 것은 그러나

$("#tags").autocomplete({ 
     source: "search.php", 
     minLength: 2, 
     select: function(event, ui) { 
      window.location = ("customers.php?action=view&cid=" + item.id) 
      //$('#tags').val(ui.item.id); 
      //return false; 
     } 
    }) 
    .data("autocomplete")._renderItem = function(ul, item) { 
    return $("<li></li>") 
     .data("item.autocomplete", item) 
     .append("<a href='customers.php?action=view&cid="+ item.id + "'>"+ item.label + "</a>") 
     .appendTo(ul); 
    }; 

가이 오류를 제공합니다

<?php 
// connect to mysql 
require_once('includes/connect.php'); 
// include config 
include('includes/config.php'); 

$nameser = $_GET['term']; 

$names = array(); 
$result = mysql_query("SELECT name,customerid FROM customers WHERE name LIKE '%".$nameser."%' ORDER BY name ASC"); 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
    $row_array['id'] = $row['customerid']; 
    $row_array['value'] = htmlentities($row['name']); 

    array_push($names,$row_array); 
    } 

echo json_encode($names); 

?> 

자바 스크립트 부분은 다음과 같습니다 결과 목록에서 뭔가를 클릭하면 URL로 리디렉션됩니다.

customers.php?action=view&cid={CustomerID} 

누구나 아이디어가 있습니까?

편집 :

올바른 자바 스크립트 코드 작동 :

$("#tags").autocomplete({ 
    source: "search.php", 
    minLength: 2, 
    select: function(event, ui) { 
     window.location = ("customers.php?action=view&cid=" + ui.item.id) 
     //$('#tags').val(ui.item.id); 
     //return false; 
    } 
}) 
.data("autocomplete")._renderItem = function(ul, item) { 
return $("<li></li>") 
    .data("item.autocomplete", item) 
    .append("<a href='customers.php?action=view&cid="+ item.id + "'>"+ item.label + "</a>") 
    .appendTo(ul); 
}; 
+0

라인 5, js : 항목이 무엇입니까? 전역 변수? – madfriend

+0

@Alex : 실제로 모르겠지만 - 13 행에서 작동합니다. 스크립트는 다른 게시물의 일부 복사/붙여 넣기입니다. –

+0

라인 13'item'은 _renderItem 함수에 전달 된 인자이므로 괜찮습니다. 'select' 이벤트 전에 어떻게 링크가 보이는지 게시하십시오. – madfriend

답변

3

내가 키가 유효한 URL 값을 얻을 수 있습니다 jsfiddle. you can see it here:

에서 동작하는 예제와 데모가 자동 선택 선택 방법. console.log에 ui 값을 입력해야 사용할 수있는 것을 볼 수 있습니다. 표준 ui.item.value 값을 사용하여 사용자를 다른 페이지로 보낼 수있었습니다.

1.) 입력 상자로 이동 한 다음 ' "를 입력 :

내 데모를 테스트합니다. 2.) "http://www.utexas.edu"를 선택하십시오. 3.) 새 페이지가로드됩니다.

+0

감사합니다. 내가해야 할 일은'item.id'에'ui '를 추가하는 것뿐입니다! –

+0

문제 없습니다. 언제든지. – chrisvillanueva