user659025
user659025

Reputation:

jQuery UI Autocomplete: How to storage the items not only the values?

this is my code. You can also watch this jsfiddle for a live demo.

$(function()
{
    var source = [{id:1, value: "One"},
                 {id:2, value: "Two"},
                 {id:3, value: "Three"},
                 {id:4, value: "Four"}];

    $("input").autocomplete({
        source: function(request, response)
        {
            var term = request.term.split(/,\s*/).pop();

            response($.ui.autocomplete.filter(source, term));
        },
        select: function(e, ui)
        {
            var terms = this.value.split(/,\s*/);

            terms.pop();
            terms.push(ui.item.value, "");

            this.value = terms.join(", ");

            return false;
        }
    });
});

As you can see I have implemented a simple multiselect. My problem is, that I don't only need the values from the original datasource, but also their IDs. How can this be done? How can I get all items I have selected in their entirety after I'm done with the autocomplete field?

Upvotes: 2

Views: 185

Answers (1)

Karthi Keyan
Karthi Keyan

Reputation: 4393

Take a look at this DEMO link this will you to represent the selected value and it id in autocomplete.

terms.push(ui.item.id+"="+ui.item.value, "");

i hope this will help you more.

EDIT: i have updated this fiddle.

Upvotes: 1

Related Questions