我有4个选择框,当我改变第一个时,做一些像空的东西,追加并为下一个设置新值.
因为我使用select2只能使用$ .select2('val','value')设置它;
只是该命令在另一个选择上触发更改事件并执行级联更改.
请注意.empty()和append()不会触发(我喜欢),甚至.val()也不应该触发它,但是当使用select2时,你无法使用它来访问新的val.
代码在这里:
function anidado(selectorOrigen,selectorDestino) {
id = selectorOrigen;
alert(id);
destino = "#"+selectorDestino;
valor = $('#'+id).find(":selected").val();
$.ajax({
method: "GET",
url: "blanco2.php",
data: { select: id, valor: valor }
})
.done(function( msg ) {
obj = $.parseJSON( msg );
if (obj.length>0) {
$(destino).empty().append('<option value="x">Select an ---</option>').select2("val", "x");
$.each(obj,function(index) {
valor = obj[index].codigo;
texto = obj[index].descripcion;
$(destino).append('<option value=' + valor + '>' + texto + '</option>');
$(destino).prop("readonly",false);
});
} else {
$(destino).empty().append('<option value=""></option>').select2("val", "");
}
});
return false;
} …Run Code Online (Sandbox Code Playgroud)