When a dropdown's value changes, I need the values of two textboxes to change, based on the new value of the dropdown, without refreshing the page.
This should be dead easy, but I'm having trouble figuring out what makes AJAX tick. I seem to be able to get the value, but I can't set the textbox.
This similar question didn't help me. Neither did this.
JavaScript:
// Dropdown ID = PatientID, textbox ID = DoB
var patient = $("#PatientID");
patient.change(function () {
var newDob;
$.ajax({
url: '@(Url.Action("PatientDob"))?patientId=' + patient.val(),
type: "GET",
success: function (result) {
// Displays correct result
alert(result);
newDob = result;
},
error: function (xhr, status, err) {
alert(err);
}
});
// Causes the box to become blank, HTML value attribute doesn't change
$("#DoB").val(newDob);
});
Controller:
[HttpGet]
public string PatientDob(int patientId)
{
return db.Patients.Find(patientId).DateOfBirth.ToString("MM/dd/yyyy");
}
successwill run at some point in the future when the request is complete, currently$("#DoB").val(newDob);is called before that happens. Set the value where you currentlyalert().val()at the end from a previous stump and autopilot told me it needed to be set there.