function getNamefromDb(){
var dataString = "metodo=getNames";
$.ajax({
type : "GET",
url : "NameList.do",
data : dataString,
dataType: "json",
async: false,
contentType: "application/json; charset=utf-8",
success : function(data) {
if (data.success == false) {
swal({
title: data.message,
html: true,
});
console.log("Fail"+data.message);
} else {
console.log("Success");
$.each(JSON.parse(data.message), function(key, value) {
$('#selectBoxId').append($("<option/>", {
value: key,
text: value
}));
});
$("#selectBoxId").multiselect('rebuild');
}
}
});
}
<select id="selectBoxId" multiple="multiple" onclick="javascript:getNamefromDb();">
答案 0 :(得分:0)
您可以这样做:
let dropdown = document.getElementById('selectBoxId');
if (data.success){
const data = JSON.parse(request.responseText);
let option;
for (let i = 0; i < data.length; i++) {
option = document.createElement('option');
option.text = data[i].value;
option.value = data[i].key;
dropdown.add(option);
}}
答案 1 :(得分:0)
$('#selectBoxId').change(function() {
var dataString = "{metodo:getNames,value:$(this).val()};
$.ajax({
type: "GET",
url: "NameList.do",
data: dataString,
dataType: "json"
async: false,
success: function(response) {
data = response;
return response;
},
if (data.success == false) {
swal({
title: data.message,
html: true,
});
console.log("Fail" + data.message);
} else {
console.log("Success");
$.each(JSON.parse(data.message), function(key, value) {
$('#selectBoxId').append($("<option/>", {
value: key,
text: value
}));
});
$("#selectBoxId").multiselect('rebuild');
}
}
});
}