javascript - How do I pass a multidimensional array to jquery ui autocomplete? - Stack Overflow

I have an autoplete field and have it's source set to an array of friends.GetFollowing(9, function

I have an autoplete field and have it's source set to an array of friends.

 GetFollowing(9, function(response)
                    {
                        if (response)
                        {
                            donor.following = response;
                            for (var i = 0; i < response.length -1; i++)
                            {
                                // make sure we have an image to display
                                if (response[i].image != null)
                                {
                                    $("#following").prepend("<li title='" + response[i].firstname + " " + response[i].lastname +
                                        "'><img src='<?php echo $baseUrl;?>/uploads/profile-pictures/" + response[i].image +
                                        "' alt='" + response[i].firstname + " " + response[i].lastname + "'/></li>");

                                    // add to the friends array
                                    if (! $.inArray(response[i].firstname + " " + response[i].lastname, friends))
                                        friends.push(response[i].firstname + " " + response[i].lastname);
                                }
                            }
                        }
                    });
                    GetFollowers(9, function(response)
                    {
                        if (response)
                        {
                            donor.followers = response;
                            for (var i = 0; i < response.length -1; i++)
                            {
                                // make sure we have an image to display
                                if (response[i].image != null)
                                {
                                    $("#followers").prepend("<li title='" + response[i].firstname + " " + response[i].lastname +
                                        "'><img src='<?php echo $baseUrl;?>/uploads/profile-pictures/" + response[i].image +
                                        "' alt='" + response[i].firstname + " " + response[i].lastname + "'/></li>");

                                    // Add to the friends array
                                    friends.push(response[i].firstname + " " + response[i].lastname);
                                }
                            }
                        }
                        // setup jquery-ui autoplete
                        $("#msg-to").autoplete({source: friends});
                    });

My problem is I need to associate an id with the value that is selected, how can this be done?

I have an autoplete field and have it's source set to an array of friends.

 GetFollowing(9, function(response)
                    {
                        if (response)
                        {
                            donor.following = response;
                            for (var i = 0; i < response.length -1; i++)
                            {
                                // make sure we have an image to display
                                if (response[i].image != null)
                                {
                                    $("#following").prepend("<li title='" + response[i].firstname + " " + response[i].lastname +
                                        "'><img src='<?php echo $baseUrl;?>/uploads/profile-pictures/" + response[i].image +
                                        "' alt='" + response[i].firstname + " " + response[i].lastname + "'/></li>");

                                    // add to the friends array
                                    if (! $.inArray(response[i].firstname + " " + response[i].lastname, friends))
                                        friends.push(response[i].firstname + " " + response[i].lastname);
                                }
                            }
                        }
                    });
                    GetFollowers(9, function(response)
                    {
                        if (response)
                        {
                            donor.followers = response;
                            for (var i = 0; i < response.length -1; i++)
                            {
                                // make sure we have an image to display
                                if (response[i].image != null)
                                {
                                    $("#followers").prepend("<li title='" + response[i].firstname + " " + response[i].lastname +
                                        "'><img src='<?php echo $baseUrl;?>/uploads/profile-pictures/" + response[i].image +
                                        "' alt='" + response[i].firstname + " " + response[i].lastname + "'/></li>");

                                    // Add to the friends array
                                    friends.push(response[i].firstname + " " + response[i].lastname);
                                }
                            }
                        }
                        // setup jquery-ui autoplete
                        $("#msg-to").autoplete({source: friends});
                    });

My problem is I need to associate an id with the value that is selected, how can this be done?

Share Improve this question asked Feb 27, 2012 at 23:25 Carl WeisCarl Weis 7,08215 gold badges65 silver badges86 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

http://jqueryui./demos/autoplete/

you should read the overview more.

Expected data format

The data from local data, a url or a callback can e in two variants:

  • An Array of Strings:

[ "Choice1", "Choice2" ]

  • An Array of Objects with label and value properties:

[ { label: "Choice1", value: "value1" }, ... ]

The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.

use the label for textual representation, the value for the id per item in the selection.

you should also differentiate the textbox used for the visual version (which displays words) from the data version (a hidden input which contains id's) - similar to those of the datepicker widget (aka. alternate data). this can all be manipulated using the settings.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744116971a4559219.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信