【发布时间】:2021-09-27 10:34:13
【问题描述】:
打开模态框后我正在做一些功能。 RefreshBirths() 函数需要 motherId 和 fatherId 来自 getDictionaryMother() 和 getDictionaryFather() 函数(这些在页面上呈现 my mothers 和 father 然后我可以得到refreshBirths() 函数中的值)。
我检查了refreshBirths() 太早使用我的 id,所以最后有未定义的 id。
我检查了在mother/father 方法中的.done 部分中调用我的refreshBirths() 函数,它解决了我的问题,但我更喜欢在代码的指定部分中明确地这样做。
这不能解决,但我想在那儿做这个:
$('#create-modal').on('shown.bs.modal', function() {
refreshBreeds();
getDictionaryMother();
getDictionaryFather();
$.when(getDictionaryFather()).done(function() {
refreshBirths();
});
});
这里修复了,但我更喜欢在另一个地方调用该方法
function getDictionaryMother() {
$.ajax({
url: "/admin/api/dictionary/" + "ANIMAL_MOTHER",
type: "GET",
dataType: "json"
})
.done(function(response) {
$('#mother-create').empty();
$('#mother').empty();
response.forEach(function(mother){
$('#mother-create').append('<option value='+ mother.id +'> '+ mother.value +' </option>');
$('#mother').append('<option value='+ mother.id +'> '+ mother.value +' </option>');
});
refreshBirths();
})
.fail(function(jqxhr, textStatus, errorThrown) {
displayErrorInformation("Cannot get dict: " + name + " due to: " + jqxhr.responseText);
});
}
【问题讨论】:
-
您可以添加一个回调函数作为参数,然后在方法中最后调用它,即在处理来自 ajax 请求的响应之后。
标签: javascript html jquery ajax bootstrap-5