Reputation: 1687
I have an input element that I am setting the value to be nothing initially:
<input id="fooValue" type="hidden" data-url="@Url.Action(MVC.fooController.fooValue())" value="" />
I then have an AJAX call where I retrieve a value and try to pass it into the input and set the previously blank value to this new value:
$.ajax({
type: 'GET',
url: $('#fooValue').data('url'),
success: function (data) {
$('#fooValue').val(data);
}
});
When I perform this AJAX request in the Firebug console I get back the value of 4, however when I got to get the value from the input it isn't returning anything. Can anybody help me and tell me why it would appear that the value is not being set with the AJAX request? Thanks.
Upvotes: 2
Views: 3045
Reputation: 37856
Ajax is an asynchronous request. may be you try with async: false
in your ajax function?
$.ajax({
type: 'GET',
url: $('#fooValue').data('url'),
async: false,
success: function (data) {
$('#fooValue').val(data);
}
});
Upvotes: 0
Reputation: 6023
The following post might be helpful. Although his example involves a HIDDEN field I think you may be able to fix your problem by removing value=""
from your HTML as described in the blog post.
Upvotes: 1