jQuery的自动完成 - 如何处理额外的数据? [英] jQuery autocomplete - How to handle extra data?

查看:124
本文介绍了jQuery的自动完成 - 如何处理额外的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题所困扰。我使用 jQuery的自动完成插件,以从服务器获取建议值的列表。该名单将是这样的:

I'm struggling with the following problem. I use the jQuery autocomplete plugin to get a list of suggested values from the server. The list would look like this:


Username1|UserId1
Username2|UserId2

所以,如果我开始输入U,USERNAME1名单USERNAME2弹出,符合市场预期。我可以选择的第一项和<输入> 的价值将成为USERNAME1,但我的真正的要发送到服务器的用户ID。

So if I start typing "U", a list of "Username1" and "Username2" pops up, as expected. I could chose the first item and the <input>'s value would become "Username1", but what I really want to send to the server is the user ID.

我可以以某种方式得到关注的用户名ID的举行?我打算做的文本框的变化形式的岗位。也许我只是太盲目地看到它在文档或找到它在谷歌?

Can I somehow get a hold of the ID that is following the user name? I intend to do the form post on change of the text box. Maybe I'm just too blind to see it in the docs or to find it on Google?

推荐答案

使用结果的方法自动完成插件来处理这个问题。该数据作为一个数组给回调过去了,你只需要保存数据[1] 的某个地方。事情是这样的:

Use the result method of the autocomplete plugin to handle this. The data is passed as an array to the callback and you just need to save data[1] somewhere. Something like this:

$("#my_field").autocomplete(...).result(function(event, data, formatted) {
    if (data) {
        $("#the_id").attr("value", data[1]);
    }
});

这篇关于jQuery的自动完成 - 如何处理额外的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆