我正在使用jquery的自动完成功能,它工作得很好,只是当我从下拉列表中选择某些内容时,输入字段会填充值而不是标签。
我的代码如下:
<?php
$query = mysql_query("SELECT users.* FROM users JOIN peers ON peers.peer = users.user_id WHERE peers.user_id = '".$_SESSION['id']."'")or die(mysql_error());
$count = mysql_num_rows($query);
$i = 0;
while($row = mysql_fetch_assoc($query))
{
$first[$i] = $row['first_name'];
$last[$i] = $row['last_name'];
$user_id[$i] = $row['user_id'];
$i++;
}
$data = "";
for($i=0;$i<$count;$i++)
{
if($i != ($count-1))
{
$data .= '{ value: '.$user_id[$i].', label: "'.$first[$i].' '.$last[$i].'" }, ';
} else
{
$data .= '{ value: '.$user_id[$i].', label: "'.$first[$i].' '.$last[$i].'" }';
}
}
?>
<script type="text/javascript">
$("#auto").autocomplete({
source: data,
select: function(event, ui)
{
var id = ui.item.value;
var name = ui.item.label;
}
});
</script>
<input type="text" value="Enter a connection's name" id="auto" />
答案 0 :(得分:16)
select事件的默认行为是使用ui.item.value更新输入。此代码在事件处理程序之后运行。
只需返回false或调用event.preventDefault()以防止发生这种情况。试试这个,
<script type="text/javascript">
$("#auto").autocomplete({
source: data,
select: function(event, ui)
{
var id = ui.item.value;
var name = ui.item.label;
$("#auto").val(name);
return false;
},
focus: function(event, ui){
return false;
}
});
</script>
答案 1 :(得分:2)
您可以尝试将label
放在value
之前的数组中,或者从.autocomplete() with custom results上的jQuery文档中查看此代码:
<script>
$(function() {
var projects = [
{
value: "jquery",
label: "jQuery",
desc: "the write less, do more, JavaScript library",
icon: "jquery_32x32.png"
},
{
value: "jquery-ui",
label: "jQuery UI",
desc: "the official user interface library for jQuery",
icon: "jqueryui_32x32.png"
},
{
value: "sizzlejs",
label: "Sizzle JS",
desc: "a pure-JavaScript CSS selector engine",
icon: "sizzlejs_32x32.png"
}
];
$( "#project" ).autocomplete({
minLength: 0,
source: projects,
focus: function( event, ui ) {
$( "#project" ).val( ui.item.label );
return false;
},
select: function( event, ui ) {
$( "#project" ).val( ui.item.label );
$( "#project-id" ).val( ui.item.value );
$( "#project-description" ).html( ui.item.desc );
$( "#project-icon" ).attr( "src", "images/" + ui.item.icon );
return false;
}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
.appendTo( ul );
};
});
</script>
<div class="demo">
<div id="project-label">Select a project (type "j" for a start):</div>
<img id="project-icon" src="/demos/autocomplete/images/transparent_1x1.png" class="ui-state-default"//>
<input id="project"/>
<input type="hidden" id="project-id"/>
<p id="project-description"></p>
</div>
答案 2 :(得分:0)
自动完成选择事件的默认行为是用值而不是文本填充输入字段
但是您可以使用以下代码在选择以及向下滚动选项时使用文本更新输入字段。
<script type="text/javascript">
$("#auto").autocomplete({
source: data,
select: function(event, ui)
{
var id = ui.item.value;
var text = ui.item.label;
$("#auto").val(name);
return false;
},
focus: function (event, ui) {
$("#auto").val(ui.item.label);
return false;
}
});
</script>