jQuery自动完成功能显示值而不是标签 [英] jQuery autocomplete shows value instead of label
问题描述
我正在使用 jQuery-ui自动完成.这是我的用法
I am using jQuery-ui autocomplete. Here is how i use it
<form action="go.php" method="post">
<input id="txt" type="text" />
</form>
<script>
$('#txt').autocomplete({
source: [{'label': 'milan', 'value': '1'}, {'label': 'minos', 'value': '2'}]
})
</script>
当我输入'mi'时,会出现milan
和minos
,这没问题.但是,当我关注它们时,#txt
的向下键值更改为1
(米兰的值),而不是milan
.当我专注于milan
时该如何显示.谢谢
When i type 'mi', milan
and minos
shows up.No problem with that. However when i focus on them with down key value of #txt
changes to 1
(value for milan) instead of milan
. How can i show milan
when i focus on it. Thanks
推荐答案
由于 select 事件的默认行为是显示值而不是标签,因此您只需:
- 返回假 ,如前面提到的" İsmailHakkıŞen "一样.
-或者您可以使用 event.preventDefault() 阻止事件的默认操作执行,然后写下您需要执行的操作,如下所示:
Because the default behavior of the select event is to show the value not the label, so you need to simply:
- return false as "İsmail Hakkı Şen" mentioned earlier.
- or you can use event.preventDefault() to prevent the default action of the event from executing and then write down what you need to do as follows:
$('#txt').autocomplete({
source: [{'label': 'milan', 'value': '1'}, {'label': 'minos', 'value': '2'}],
focus: function( event, ui ) {
event.preventDefault();
$('#txt').val(ui.item.label);
},
select: function( event, ui ) {
event.preventDefault();
$('#txt').val(ui.item.label);
}
});
Please refer to "Andrew Whitaker" answer in this post > Autocomplete applying value not label to textbox - as it is really helpful in understanding.
这篇关于jQuery自动完成功能显示值而不是标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!