2015-11-03 65 views
1

IM,的jquery-UI-自動完成使用以下代碼填充其它輸入

JS

<script> 
    $(function() { 
     function log(message) { 
      $("<div>").text(message).prependTo("#log"); 
      $("#log").scrollTop(0); 
     } 

     $("#input").autocomplete({ 
      source: "source.php", 
      minLength: 2, 
      select: function(event, ui) { 
       log(ui.item ? 
       "SelectedName: " + ui.item.value + " -SelectedID: " + ui.item.ID: 
       "Nothing selected, input was " + this.value); 
      } 
     }); 

PHP/HTML

<tr> 
    <td colspan="2">Input</td> 
    <td colspan="2" > 
     <input class="glowing-border" id="input"> 
    </td> 
</tr> 


<div class="ui-widget" style="margin-top:2em; font-family:Arial"> 
    Result: 
    <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div> 
</div> 

這工作完全。但我在這裏卡住了,因爲我想把結果分成兩部分。現在,在div獲取顯示:

「selectedName:theName - SelectedID:12345」

但是,我需要填寫其他輸入字段這兩個值,有北京時間輸入字段<input id="selectedText">和一個<input id="selectedID">

我該怎麼做?

2.)當我在「輸入」中鍵入somthing時,我只能從db中獲得匹配的文本。是否有可能在列表框中顯示ID?

回答

1

像這樣的東西?

$("#input").autocomplete({ 
     source: "source.php", 
     minLength: 2, 
     select: function(event, ui) { 
      log(ui.item ? 
      "SelectedName: " + ui.item.value + " -SelectedID: " + ui.item.ID: 
      "Nothing selected, input was " + this.value); 
      if (ui.item){ 
       $("#selectedText").val(ui.item.value); 
       $("#selectedID").val(ui.item.ID); 
      } 
     } 
    });