0

I need a combobox (id / name) pair, where the user selects an item, and the id is sent to the server. at the same time the combobox calls a ajax webservice to select with autocomplete

I have an almost working example, in this JSFiddle

Here is the AJAX part:

source: function(request, response) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    type: "POST",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        username: "andrebarata",
                        maxRows: 12,
                        name_startsWith: request.term
                    },
                    success: function(data) {
                        response($.map(data.geonames, function(item) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.geonameId
                            }
                        }));
                    }
                })
            },

the only problem is that when i select a value it is changing the value to the ID instead of the name.

What should i do to change this?

1
  • I'm wondering if the solution could be storing a (name/value) pair in each of the autocomplete items, and then retreiving the one i need Commented Oct 28, 2014 at 13:28

1 Answer 1

1

If I change "value: item.geonameId" by "value: item.name" it's working ...

and to get the id add a new attribute "geoid" like this :

success: function(data) {
                        response($.map(data.geonames, function(item) {
                            return {
                                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.name,
                                geoid: item.geonameId
                            }
                        }));
                    }
                })
            },
            select: function(event, ui) {
                alert(ui.item.value+" , "+ ui.item.geoid);
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but the ID is lost, as you can see in the popup message.
i think the autocomplete widget only stores a simple list, not a (name/value) pair list
Ok but you can do it by add a new attribute like geoid and put your id data in it. So you have this : "..value: item.name, geoid:item.geonameId ..." and in the alert message : "ui.item.value+" , "+ ui.item.geoid"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.