jQuery的,自动完成使用JSON,ID的VS显示值 [英] Jquery, Autocomplete using json, id's vs display values

查看:87
本文介绍了jQuery的,自动完成使用JSON,ID的VS显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种复杂的自动完成的问题。这是一个网站我工作的一个消息系统。我希望它的工作,你的用户名输入,它回来与他们的图像的拇指和他们的名字和他们的ID。然后,当你选择它,我想让它显示的用户名,但是当它回发我希望它发回其ID(作为用户的名称不是唯一的)。

I have kind of a complicated autocomplete issue. This is for a messaging system for a website I'm working on. I want it to work where you type in a user's name, it comes back with a thumb of their image and their name and their id. Then, when you select it, I want it to show the users name, but when it posts back I want it to send back their ID (as a user's name is not unique).

我开始与 HTTP://blog.schuager .COM / 2008/09 / jQuery的 - 自动完成 - JSON-apsnet-mvc.html 作为一种方法。不过,我利用在#1作为tageditor.js我扩展,只是因为我喜欢它是如何工作的。

I started with http://blog.schuager.com/2008/09/jquery-autocomplete-json-apsnet-mvc.html as an approach. However, I am using the tageditor.js from Stackoverflow as my extender, just because I like how it works.

标记编辑器下面链接。我认为这是一个旧版本。

the tag editor is linked below. I think it's an older version.

我们正在使用MVC 1.0。这里是我的code:

We are using MVC 1.0. Here's my code:

public ActionResult Recipients(string prefix, int limit)
        {
            IList<UserProfile> profiles = profileRepository.GetUsers(prefix, limit);

            var result = from p in profiles
                         select new
                         {
                             p.ProfileId,
                             p.FullName,
                             ImageUrl = GetImageUrl(p)
                         };

            return Json(result);
        }

下面是该脚本的页面上

<script type="text/javascript">
$(document).ready( function() {  
    $('#recipients').autocomplete('<%=Url.Action("Recipients", "Filter") %>', {      
        dataType: 'json',      
        parse: function(data) {
            var rows = new Array();          
            for(var i=0; i < data.length; i++) {
                rows[i] = { data: data[i], value: data[i].ProfileId, result: data[i].FullName };
            }          
            return rows;      
        },      
        formatItem: function(row, i, n) {
            return '<table><tr><td valign="top"><img src="' + row.ImageUrl + '" /></td><td valign="top">' + row.FullName + '</td></tr></table>';
        },      
        max: 20,
        highlightItem: true,
        multiple: true,
        multipleSeparator: ";",
        matchContains: true,
        scroll: true,
        scrollHeight: 300
     });
});
</script>

那么,什么情况是回电的作品,我的列表显示的图像和用户名,当我选择一个,它与delimter会将用户的全名在文本框中。然而,当我提交表单,只有人名被送回,而不是个人资料的ID。如何获得该ID的背部没有在文本框中显示它们任何想法?

So, what happens is the call back works, my list shows the image and user name, and when I select one, it puts the user's full name in the text box with the delimter. However, when I submit the form, only the names are sent back and not the profile ids. Any ideas on how to get the ID's back without displaying them in the text box?

编辑:
下面是我使用tageditor.js版本 http://www.gotroleplay.com/scripts/ tageditor.js

推荐答案

我知道这是跛,但我一直是(a)后从结果处理(不formatResult,对此,我的理解是,只是格式化数据结果为将在文本框中,或(b)在一个隐藏字段坚持的价值 - 从结果再处理 - 用于发布

I know it's lame, but I always either (a) post the data from the result handler (NOT the formatResult, which, as I understand it, just formats the result for putting in the textbox, or (b) stick the value in a hidden field -- again from the result handler -- for posting.

    $('#recipients').autocomplete({options})
     .result(function(event, data, formatted) {
         $("#hidden_field").val( data.ProfileId );
// or just post the data from in here...
    });

什么的。请让我们知道,如果你找到一个更好的办法...

or something. Please let us know if you find a better way...

显然,直接从自动完成结果发布仅在非常特殊的情况适当

Obviously, posting directly from the autocomplete 'result' is only appropriate in very specific scenarios

这篇关于jQuery的,自动完成使用JSON,ID的VS显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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