I use jquery to retrieve via ajax request a number of values that I use to populate input fields. After retrieving these fields I need to use their new values in another ajax call, but I see that I get values that were in the field before my ajax call.
$("#clickme").function(e) {
e.preventDefault()
retrieveValues()
compareValues()
}
function retrieveValues {
paramList = {}
..... snip .....
$.ajax({
url: myControllerAction,
type: "POST",
dataType: "json",
data: JSON.stringify(paramList),
contentType: "application/json; charset=utf-8",
success: function (result) {
$("#myFieldOne").val(result["myFieldOne"]);
$("#myFieldOne").val(result["myFieldOne"]);
$("#myFieldThree").val(result["myFieldThree"]);
}
},
error: function () {
alert("error");
}
});
function compareValues {
paramList = {}
paramList["myFieldOne"] = $("#myFieldOne").val();
paramList["myFieldTwo"] = $("#myFieldTwo").val();
paramList["myFieldThree"] = $("#myFieldThree").val();
paramList = {}
..... snip ....
$.ajax({
url: myOtherControllerAction,
type: "POST",
dataType: "json",
data: JSON.stringify(paramList),
contentType: "application/json; charset=utf-8",
success: function (result) {
.....
some Business Logic
.....
}
},
error: function () {
alert("error");
}
});
}
Before clicking on "clickme" I have
<input name="myFieldOne" id="myFieldOne" value="default value #1">
<input name="myFieldTwo" id="myFieldTwo" value="default value #2">
<input name="myFieldThree" id="myFieldThree" value="default value #3">
and the function retrieveValues SHOULD SET these three fields with new values "New Value #1", "New Value #2", "New Value #3".
After returning from retrieveValues() I call compareValues(), but Jquery objects still contain default values. I seems that field values are actually changed only after the end of the click event. Function compareValues() sends wrong data on the first click, values are compared wrong, then if I click again on "clickme" values are compared right.
Is there any way to correct this and have jquery recognize on the fly my new values in input fields?
Francesco