我有以下功能:
function getVenues(categoryId) {
var venues = "";
$.getJSON('@Url.Action("GetVenues", "Team")' + "?categoryId=" + categoryId, function (data) {
success: $.each(data, function (a, b) {
venues += "<option value='" + b.Value + "'>" + b.Text + "</option>";
});
$('#venuelist').html(venues);
});
var venueId= $('#venuelist').val();
getVenueNotes(categoryId, venueId);
};
问题是当$('#venuelist')。html(场地)时,下拉列表不会立即填充。叫做。所以没有它,我无法将valueId传递给下一个getVenueNotes()函数。但是,当我让它运行时,正在填充下拉列表。在调试和逐步执行深度jquery代码时,它会在大约15步之后填充,这段代码开始于:
}p&&o(s,c,p,l)},n.async?4===u.readyState?setTimeout(r):(a=++W.....
所以我猜测与readystate有关,但是如何在数据被引入之前暂停执行我的函数?
答案 0 :(得分:0)
通过将done()方法附加到.getJSON()来实现它:
function getVenues(categoryId) {
var venues = "";
$.getJSON('@Url.Action("GetVenues", "Team")' + "?categoryId=" + categoryId, function(data) {
success: $.each(data, function(a, b) {
venues += "<option value='" + b.Value + "'>" + b.Text + "</option>";
});
}).done(function () {
$('#venuelist').html(venues);
var venueId = $('#venuelist').val();
getVenueNotes(categoryId, venueId);
});
};