我有一个带有自动完成功能的简单文本输入:
<input type="text" class="span4" id="autoc1" name="agent" value="">
我正在使用以下 jquery 代码来执行自动完成并带回数据。然后单击一下,我想用返回的数据填充两个输入。除了没有填充自动完成的实际字段之外,一切都按原样工作。
查询:
$(function() {
// input id of field with autoc on
$( "#autoc1" ).autocomplete({
// php mysql data get
source: "/pages/includes/getAgent.php",
minLength: 2,
select: function( event, ui ) {
//alert(ui.item.agent_name); // shows correct info, here only to test
//tried $(this) as below - didn't work
//$(this).val(ui.item.agent_name);
$('#autoc1').val(ui.item.agent_name); // tried with and without this, didn't work
$('#comm').val(ui.item.commission_percent); // this works perfectly!!
}
}).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.append( "<a>" + item.agent_name + "</a>" )
.click(function(){$('#autoc1').val(item.agent_name)}) // added this. didn't work
.appendTo( ul );
};
});
如果有帮助,这是返回的 JSON:
[{"0":"agent a","agent_name":"agent a","1":"15","commission_percent":"15"},
{"0":"agent b","agent_name":"agent b","1":"10","commission_percent":"10"}]
完全没有想法!
编辑
更多html,基本形式,简单
<form class="add_booking" method="post">
<input type="text" placeholder="Date" class="span2" id="datepicker" name="date" value="<?=$showDate?>">
<input type="text" placeholder="Booking Agent" class="span4 here" id="autoc1" name="agent" value="<?=$ds['agent']?>">
<input type="text" placeholder="15" class="span1" name="commission" id="comm" value="<?=$ds['show_comm_percent']?>">%
etc etc </form>