Hi, I have this problem with two nested select2s (one depends on the other), the first thing is that I filled a datatable.net with the data and I passed them through oneclick to be able to edit them:
EditarTrabajador = function ( CodOficina, DepOficina) {
$.post("/Oficina/OficinaGeneral",
function (data) {
$.each(data, function (i, item) {
$('#Ecbooficinageneral').append('<option value="' + item.Cod_Oficina_G + '">' + item.Nom_OFicina_G + '</option>');
$("#Ecbooficinageneral").val(DepOficina);
$('#Ecbooficinageneral').change();
});
});
$('#Ecbooficinageneral').on('change', function () {
var codofi = $('#Ecbooficinageneral').val();
$('#Ecbooficinadep').val(null).trigger('change');
$('#Ecbooficinadep').html('<select class="form-control select2" id="cbooficinadep" style="width: 100%;">' +
'<option selected="selected" value="">Seleccione Oficina Dependiente</option>' +
'</select>');
$.post("/Oficina/OficinaDep",
{ codoficina: codofi },
function (data) {
$.each(data, function (i, item) {
$('#Ecbooficinadep').append('<option value="' + item.Cod_Oficina_D + '">' + item.Nom_Oficina_D + '</option');
$("#Ecbooficinadep").val(CodOficina);
$('#Ecbooficinadep').change();
});
});
});
}
The problem is that when filling the first combo and put:
$("#Ecbooficinageneral").val(DepOficina);
$('#Ecbooficinageneral').change();
the change causes the same data to be filled in the following combo several times, how can I load the first select with the option to edit, clean the second select and load it with the option to edit anyway?