2014-02-25 166 views
0

我想顯示一個多欄自動提示。 我希望它從我的數據庫返回suburb,state當用戶搜索它。 目前只返回suburbjquery自動完成多列

它也填充了其他兩列,但我希望它在填充它們之前顯示另外兩列columsn的值。

$st = DB::singleton() 
    ->prepare(
     'select postcode, suburb, state ' . 
     'from postcode_db ' . 
     'where suburb like :suburb ' . 
     'order by suburb asc ' . 
     'limit 0,10'); 

$suburbq = $_REQUEST['term'] . '%'; 
$st->bindParam(':suburb', $suburbq, PDO::PARAM_STR); 

$data = array(); 
if ($st->execute()) 
{ 
while ($row = $st->fetch(PDO::FETCH_OBJ)) 
{ 
    $data[] = array(
     'postcode' => $row->postcode , 
     'value' => $row->suburb , 
     'state' => $row->state 
    ); 
} 
} 
echo json_encode($data); 
flush(); ` 

我的JQuery

jQuery(document).ready(function(){ 
     $('.postcode').autocomplete({ 
      source:'../../assets/php/data.php', 
      minLength:2, 
      width: 300, 

      select:function(event, ui) 
      { 
       // when a zipcode is selected, populate related fields in this form 
       this.form.suburb.value = ui.item.suburb; 
       this.form.postcode.value = ui.item.postcode; 
       this.form.state.value = ui.item.state; 
      } 
     }); 
    }); 


</script> 

我的形式

<form onsubmit="return false;"> 
Enter a Postcode: 
<input id="postcode" type="text" /> 

City: 
<input id="suburb" type="text" class="postcode"/> 

State: 
<input id="state" type="text" /> 

回答

0

我能得到通過這樣

它同時顯示 suburb

但現在的文本字段中輸入將同時存儲suburbstate 如果有人有更好的解決方案,請張貼。