2012-09-13 4 views
0

좋아, 그래서 나는 미국의 위치와 주에 대한 내 데이터베이스로 작업하는 jQuery 자동 완성을 사용하고있다. 목표는 사용자가 도시, 마을 등을 입력하기 시작하고 City (위치 내 DB에), 주 (내 DB에있는 지역) 우편 (내 DB에 postalCode) 그들이 하나를 선택하면 도시의 주 및 우편 번호 필드를 채 웁니다 .. 내 문제는 내가 옵션을 선택하면 주 및 우편 번호 코드를 올바르게 채우지 만, 도시 필드는 우편 번호의 값을 취하고 그걸 알아낼 수 없습니다. 여기 내 JS 코드는 내 DB 데이터를 가져 오는 별도의 PHP 파일 다음에 있습니다. JS보다는 PHP에서하지만 당신은 실수를하게 될 것입니다 결코 알지 못합니다 ...jQuery 자동 완성 필드에 잘못된 값 넣기

JS/jQuery 파일 :

$(document).ready(function(){ 
var ac_config = { 
    source: "autocomplete-l.php", 
    select: function(event, ui){ 
     $("#scity").val(ui.item.location); 
        $("#sstate").val(ui.item.region); 
     $("#szip").val(ui.item.postalCode); 
    }, 
    minLength:1 
}; 
$("#scity").autocomplete(ac_config); 
}); 

그리고 PHP 파일 : 선택 기능 내부

$cities = array(); 
$sth = $dbh->prepare("SELECT * FROM locations WHERE country = 'US'"); 
$sth->execute(); 

while($row = $sth->fetch(PDO::FETCH_ASSOC)) { 
$cities[]=$row; 
} 

$term = trim(strip_tags($_GET['term'])); 

$matches = array(); 
foreach($cities as $city){ 
if((stripos($city['location'], $term) !== false)){ 
    // Add the necessary "value" and "label" fields and append to result set 
    $city['value'] = $city['location']; 
    $city['value'] = $city['region']; 
    $city['value'] = $city['postalCode']; 
    $city['label'] = "{$city['location']}, {$city['region']}{$city['postalCode']}"; 
    $matches[] = $city; 
    } 
} 

$matches = array_slice($matches, 0, 100); 
print json_encode($matches); 
+0

나는 당신이이 문장에서 쉼표 누락 것 같아요 "{$ 도시 [ '위치']}, {$ 도시 [ '지역'($ .autocomplete 자동 $("#scity")의 값을 설정하기 때문에) ]} {$ city [ 'region']}과 {$ city [ 'postalCode']} 사이에 {$ city [ 'postalCode']} " json으로 인코딩 한 결과가 무엇이 아닌가? –

+0

입력란을 시작할 때 목록에 표시되는 내용을 결정할 수 없습니다. 도시, 주 우편 번호, 일반 우편 주소와 같은 특정 우편 번호를 조작 할 수 있습니다.이 경우 주소와 같은 방식으로 데이터를 표시 할 수 있습니다. . – forevermetal02

답변

2

전화 event.preventDefault();.

그렇지 않으면 automplete는 기본적으로 조치를 취한다 :

메뉴

입력 -에서 사용자가 선택한 어떤 후 입력에 소자 삽입한다

요소는 $("#scity")

$("#scity").autocomplete(ac_config); 
$city['value'] = $city['location']; 
: 당신 만이 사용할 수 있습니다 $city['value']

을 덮어 ...

$city['value'] = $city['location']; 
$city['value'] = $city['region']; 
$city['value'] = $city['postalCode']; 

:

$city['value'] = $city['postalCode']; 

주이 라인 : (10)과 값은 우편 코드입니다

을 입력하고 선택 기능에서 해당 행을 삭제하십시오.

$("#scity").val(ui.item.location); 

+0

고쳐 주셔서 감사합니다. 나중에 참조 할 수 있도록 설명해 주시겠습니까? – forevermetal02

+0

내 수정 된 답변보기를 참조하십시오. –