Javascript jquery自动完成获取所选项目文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6422235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery autocomplete get selected item text
提问by rlee923
I am wondering how to grab the selected item's text value on jquery autocomplete.
我想知道如何在 jquery 自动完成上获取所选项目的文本值。
I have initialised jquery as following :
我已经初始化 jquery 如下:
$(document).ready(function (){
$("input#autocomplete").autocomplete({
source: postcodelist,
select: function (event, ui) {
AutoCompleteSelectHandler(event, ui)
}
});
});
And I have created a function function AutoCompleteSelectHandler(event, ui) { }.
我创建了一个函数函数 AutoCompleteSelectHandler(event, ui) { }。
Inside this function I want to some extra handling to feed data into correct textboxes but I can't figure out how to grab the text value of the selected item.
在这个函数中,我想要一些额外的处理来将数据输入正确的文本框,但我不知道如何获取所选项目的文本值。
I did google a bit and tried examples but I can't seem to get it working.
我做了一些谷歌并尝试了一些例子,但我似乎无法让它工作。
Any help will be much appreciated.
任何帮助都感激不尽。
Thanks a lot advance.
非常感谢提前。
回答by GeertvdC
The ui
parameter has an item
property with the selected text
该ui
参数有一个item
带有所选文本的属性
function AutoCompleteSelectHandler(event, ui)
{
var selectedObj = ui.item;
alert(selectedObj.value);
}
source: http://jqueryui.com/demos/autocomplete/#event-selectgo to tab "Events" and then event "Select"
来源:http: //jqueryui.com/demos/autocomplete/#event-select转到选项卡“事件”,然后事件“选择”
回答by onhundper
// list of clients
$( "#client" ).autocomplete({
source: "lib/getClientList.php",
minLength: 2,
select: function(event, ui) {
alert(ui.item.value);
}
})
;
;
The ui.item.valueis the reference that jquery utilizes to assign a value to the input
该ui.item.value是参考该jquery的利用将值分配到所述输入
回答by jAndy
You don't need to apply an anonymous function as a wrap, you can directly pass the function ref.
您不需要将匿名函数应用为包装,您可以直接传递函数 ref。
$(document).ready(function (){
$("input#autocomplete").autocomplete({
source: postcodelist,
select: AutoCompleteSelectHandler
});
});
Within that method, you can either access this
or event.target
to get the current value, both values are referencing the input element:
在该方法中,您可以访问this
或event.target
获取当前值,这两个值都引用输入元素:
function AutoCompleteSelectHandler(event, ui) {
alert( $(event.target).val() );
alert( $(this).val() );
// alert( this.value );
}
回答by kinakuta
You just grab the value from the input in the same way you would if the user had typed it in themself:
您只需按照用户自己输入的方式从输入中获取值:
$('input#autocomplete').val()