如何执行传递 id 而不是名称的 jQuery 自动完成?

发布于 2024-10-06 09:37:30 字数 361 浏览 0 评论 0原文

我一直在寻找 jQuery UI 自动完成,因为我需要 但问题是我需要传递已完成名称的 id。让我解释一下:

我有一个名称数组:

$names = array(
    array('name' => 'John', id => '1'),
    array('name' => 'Sarah', id => '2'),
    array('name' => 'Josh', id => '3)
);

然后,使用 AJAX,输入将使用该数组的一个名称自动完成,但是当我提交时,我需要 id 而不是名称。我该如何管理?

提前致谢

I was looking for jQuery UI Autocomplete because I needed on a <input /> but the problem is that I need to pass the id of the name completed. Let me explain:

I have an array of names:

$names = array(
    array('name' => 'John', id => '1'),
    array('name' => 'Sarah', id => '2'),
    array('name' => 'Josh', id => '3)
);

And then, with AJAX, the input will be autocompleted with one name of the array but then, when I submit, I need the id and not the name. How can I manage?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-10-13 09:37:30

JQuery UI Autocomplete 控件可以接受搜索结果列表作为一组 Display/Value 对象。

根据记忆,您按以下格式创建一个 Json 对象...

[
    {label:"Display Name for result 1", value:1},
    {label:"Display Name for result 2", value:2},
    {label:"Display Name for result 3", value:3}
]

label 属性包含要显示的文本,值proberty 包含 id。当用户选择搜索结果时可以检查这些值。该控件有一个您可以侦听的 select 事件。

The JQuery UI Autocomplete control can accept the list of search results as a set of Display/Value objects.

From memory, you create a Json object in the following format...

[
    {label:"Display Name for result 1", value:1},
    {label:"Display Name for result 2", value:2},
    {label:"Display Name for result 3", value:3}
]

The label property contains the text to display and the value proberty contains the id. These values can be inspected when a search result is selected by the user. The control has a select event that you can listen for.

酒与心事 2024-10-13 09:37:30

您需要将 id 保存到隐藏字段中。

您可以通过“选择”事件来实现这一点。 http://jqueryui.com/demos/autocomplete/#event-select

此示例显示选择事件 < a href="http://jqueryui.com/demos/autocomplete/#custom-data">http://jqueryui.com/demos/autocomplete/#custom-data

所以想法是做类似的事情这:

        select: function( event, ui ) {
            $( "#project-id" ).val( ui.item.value );
            return false;
        }

Your need to save the id into a hidden field.

You can achieve that on the "select" event. http://jqueryui.com/demos/autocomplete/#event-select

This example shows the select event http://jqueryui.com/demos/autocomplete/#custom-data

So the idea is to do something like this:

        select: function( event, ui ) {
            $( "#project-id" ).val( ui.item.value );
            return false;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文