我使用以下代码,
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?
答案 0 :(得分: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);
}
}
});