0

auto complete is working fine. I put a hidden value for saving the value of auto complete by this method my code :

$("#cityOp").autocomplete({
        source : function(request, response) {      

        var city_value = jQuery("#cityOp").val();


        $.ajax({
            url: "city.html",

            dataType: "json", 
            data : {
                filter : city_value
            },           


            success : function(data) {

                response(jQuery.map(data.cities,function(item) {
                                    return {
                                        value : item.locationName,
                                        key : item.locationId

                                    };
                                }));
                },
select : function(event, ui) {

         $("#theHidden").val(ui.item.key) ;
    }

    }); 

then I want to get this location Id for saving the value of location , so I tried the as:

save(){
var locationValue=$("#theHidden").val(); 
//other saving codes
}

but I got here locationValue is undefined .

How I get the this hidden value in save function ? autocomplete function is in document on ready and save function is in a js.

5
  • 1
    is select function firing? put alert(ui.item.locationId); in select function. i would not put selected value in separate control but in calling autocomplete: $(this).data("value", ui.item.locationId); Commented Feb 20, 2012 at 5:45
  • Is your save() function defined properly? With the function keyword? Is the value coming in and being set properly? Is it being set undefined in the first place, you can check in your browsers debugger. Commented Feb 20, 2012 at 5:46
  • @elrado I had amistake in select function I changed $("#theHidden").val(ui.item.locationId) ; into $("#theHidden").val(ui.item.key) ; put alert as alert(ui.item.key); then alert showing the hidden value Commented Feb 20, 2012 at 5:53
  • but didn't get in save function Commented Feb 20, 2012 at 5:53
  • any solution ? pls help Commented Feb 20, 2012 at 6:03

2 Answers 2

1

this solved

select : function(event, ui) {

        setLocationValue(ui.item.key);
    }

in js

var locationValue;

function setLocationValue(value){
    locationValue=value;
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the response of the $ajax method you are mapping the data to a JSON object with only two properties, value and key.

Then, in the select, you are using a property that doesn't exist anymore.

If you want those original values to still exist, you should map them as well. Alternatively, you could save the id to the hidden field like this:

$("#theHidden").val(ui.item.key);

Let me know if that doesn't work for you.

1 Comment

my question is, in save function how to get the hidden value ? I put like var locationValue=$("#theHidden").val(ui.item.key); then got "ui is not defined".

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.