我有以下HTML多选代码:
<select class="multi-select" multiple="multiple" id="multiSelectId">
我正在尝试使用ajax动态添加元素:
function TestFunction(value) {
const selectMembers = $("#multiSelectId");
selectMembers.empty();
selectMembers.append('<option value="val">test1</option>');
$.ajax({
url: '@Url.Action("GetMemberById", "SystemAdmin")',
data: {
'memberId': value
},
success: function(data) {
selectMembers.append('<option value="val">test2</option>');
alert("test3");
}
});
selectMembers.multiSelect('refresh');
}
所以奇怪的是,test1和test3工作正常,而test2根本不起作用,我无法找到它无法正常工作的原因。有人有想法吗?
答案 0 :(得分:0)
您需要刷新success
回调处理程序。请记住ajax()
是异步的。
function TestFunction(value) {
const selectMembers = $("#multiSelectId");
selectMembers.empty();
selectMembers.append('<option value="val">test1</option>');
$.ajax({
url: '@Url.Action("GetMemberById", "SystemAdmin")',
data: {
'memberId': value
},
success: function(data) {
selectMembers.append('<option value="val">test2</option>');
selectMembers.multiSelect('refresh');
alert("test3");
}
});
}