我遇到了JQuery的问题,我有一个多选项,我可以通过两种方式填充,手动从另一个选择中使用添加按钮获取一些值,并动态地解析从spring调用返回的json。 我手动添加它时没有问题,但是,当我动态填充select时,JQuery代码不会获取任何值,尽管在html代码中,select中有值。
这是我的代码:
空html选择
<div id="enti_disp_box">
<label>Enti disponibili</label>
<select id="ente" multiple> </select>
<button class="btn" onclick="addEnteInBox();" type="button">Aggiungi</button>
</div>
<div id="enti_att_box">
<label>Enti attivi*</label>
<select id="entiAttivi" multiple></select>
<button class="btn" onclick="removeEnteInBox();" type="button">Rimuovi</button>
</div>
JQuery用于手动填充第二个选择
function addEnteInBox(){
var selectedOptions = document.getElementById("ente");
for (var i = 0; i < selectedOptions.length; i++) {
var opt = selectedOptions[i];
if (opt.selected) {
document.getElementById("entiAttivi").appendChild(opt);
i--;
}
}
}
function removeEnteInBox(){
var x = document.getElementById("entiAttivi");
x.remove(x.selectedIndex);
}
JQuery用于动态填充第二个选择
function getEntiByIdUtente(idutente) {
var action = "getEntiByidUtente";
var payload = {
"idUtente": idutente,
"action": action,
"token": token
};
$.ajax({
type: "POST",
url: '../service/rest/enti/management_utenti',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(payload),
resourceType: 'json',
success: function(obj, textstatus) {
obj = obj.trim();
var json = JSON.parse(obj);
//parse response
if (obj.stato == 'error') {
alert('Errore');
} else {
$('#entiAttivi').empty();
//fetch obj.data and populate table
$(json.data).each(function() {
$("#piva").val(this.piva);
$("#codiceipa").val(this.codiceipa);
$('#entiAttivi').append($('<option>', {
value: this.idente,
text: this.ragionesociale
}));
});
}
return json;
},
error: function(obj, textstatus) {
alert('Errore di comunicazione col server!');
}
});
}
JQuery用于获取第二个选择的值
var entiList = $("#entiAttivi").val();
答案 0 :(得分:1)
这条线似乎错了,它不适合我
$('#entiAttivi').append($('<option>', {
value: this.idente,
text: this.ragionesociale
}));
你会尝试用
替换吗?$('#entiAttivi').append($('<option value="' + this.idente + '">' + this.regionesociale + '</option>');
追加,试图创建一个以json为父的选项,这是行不通的。请试试我的代码。