1

The following code works flawlessly if I uncomment the "alert"s. This is what I've figured out for doing a set of cascading dropdowns where the user selects Country, then State, then City.

With the alerts uncommented, it's like the values haven't gotten updated yet. I've tried a lot of stuff, but I've had no luck. I'm sure I'm doing something stupid. Any help is greatly appreciated.

Thanks!

$(document).ready(function () {
    $("#Country").change(function () {
        $("#City").html("");
        var id = $("#Country").val();
        getStates(id);

//      alert("CountryId = " + id);
        var stateId = $("#State").val();
//      alert("stateId = " + stateId);
        var stateId = $("#State").val();
        alert("stateId = " + stateId);

        $(document).ready(function() {
            getCities(stateId);
        });
    });
    $("#State").change(function(){
        var id = $("#State").val();
        id = (id==null)?1:id;
            getCities(id);
    });
});
5
  • The following code works flawlessly if I uncomment the alerts contradicts With the alerts uncommented, it's like the values haven't gotten updated yet. Commented Feb 29, 2012 at 20:43
  • 1
    edit your question and put the code for getStates in it Commented Feb 29, 2012 at 20:43
  • 3
    The problem isn't here, it's in your "getStates()" and "getCities()" functions. I bet they're making ajax calls. Those are not synchronous. Commented Feb 29, 2012 at 20:44
  • 4
    Why are you defining a document.ready handler inside your document ready handler (and inside your change handler)? The document is already ready at that point. Commented Feb 29, 2012 at 20:47
  • @nnnnnn. Maybe it's because he likes spaghetti... =) Commented Feb 29, 2012 at 20:56

2 Answers 2

7

If function getStates is asynchronous request to the server then you have to add success callback to it. If not it is weird.

btw. $(document).ready inside document ready doesn't make any sense.

Sign up to request clarification or add additional context in comments.

1 Comment

:) That should not have been left in there. It was one of my attempts to try to make it work...
0

Your code does not make much sense

Perhaps this is what you mean

$(document).ready(function () {
  $("#Country").change(function () {
    var id = $(this).val();
    getStates(id);
  });
  $("#State").change(function () {
    var id = $(this).val();
    getCities(id);
  });
});

Comments

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.