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);
});
});
2 Answers 2
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.
1 Comment
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);
});
});
The following code works flawlessly if I uncomment the alertscontradictsWith the alerts uncommented, it's like the values haven't gotten updated yet.