如何获得选定的选项 jquery 自动完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4685010/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:43:27  来源:igfitidea点击:

how to get selected option jquery autocomplete

jqueryautocomplete

提问by tina

I know there's a "select" event but is not working.

我知道有一个“选择”事件但不起作用。

This is my code:

这是我的代码:

$("#Asignacion_Movimiento_OrdenCompra").autocomplete(
        "/Asignaciones/ObtenerOrdenesCompra",
        {
            extraParams: { Serial: function () { return $("#Asignacion_Movimiento_Material").val(); } },
            delay: 200,
            select: function (event, ui) {
                alert(this.value + " - " + ui.item.value);
                ObtenerDatosAdicionales();
                return true;
            }
        }
    );

I also tried adding:

我也尝试添加:

result: function (event, data, formatted) {
                alert(data);
                ObtenerDatosAdicionales();
                return true;
            }

But nothing happens...

但是什么都没有发生...

How can I get the value of the selected item by the user?

如何获取用户所选项目的值?

Thx.

谢谢。

采纳答案by tina

Done!

完毕!

I added the following to my $(document).ready function:

我在 $(document).ready 函数中添加了以下内容:

$('#autocompleteField').result(function (event, data, formatted) {
        alert(data);
});

Thank you!

谢谢!

回答by Josiah Ruddell

You are looking for the result. See here for documentation.

您正在寻找结果。有关文档,请参见此处

Like so:

像这样:

$("#Asignacion_Movimiento_OrdenCompra").autocomplete({
 /* your options here*/
}).result(function(event, data, formatted) { // result is a separate function
    alert(data);
});

回答by K232

I know this thread is a bit old, but at http://www.phpfreaks.com/forums/index.php?topic=324203.0I found a working example for select:

我知道这个线程有点旧,但是在http://www.phpfreaks.com/forums/index.php?topic=324203.0我找到了一个用于选择的工作示例:

select: function(event, ui) {
var selectedObj = ui.item;
alert(selectedObj.value); }

回答by Vinit Kadkol

Get the selected option value from the jquery Autocomplete

从 jquery 自动完成中获取选定的选项值

$("#tags").autocomplete({
source: availableTags,
select: function(event, ui) {
         //For better understanding kindly alert the below commented code
         alert(ui.toSource()); 
         var selectedObj = ui.item;
         alert(selectedObj.value);
    }
});