I have this function,
function llenarcombos() {
$.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();
});
});
$.post("/Oficina/OficinaDep",
{ codofi: CodOficina },
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();
});
});
};
I want to run it once with fillcombos (). one ();
since after this I will do the filling of combox one depending on the other with this:
$.post("/Oficina/OficinaGeneral",
function (data) {
$.each(data, function (i, item) {
$('#Ecbooficinageneral').append('<option value="' + item.Cod_Oficina_G + '">' + item.Nom_OFicina_G + '</option>');
});
});
$('#Ecbooficinageneral').change(function () {
$('#Ecbooficinageneral').each(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",
{ codofi: codofi },
function (data) {
$.each(data, function (i, item) {
$('#Ecbooficinadep').append('<option value="' + item.Cod_Oficina_D + '">' + item.Nom_Oficina_D + '</option');
});
});
});
});
the first one fills the combos to be able to edit them the second makes it load depending on the first selection of the select2,
If I do the filling just by giving the value .val .change as the first, the second select is filled as many times as the first one repeated, I wanted to avoid that
the data that is taken is by an onclick of an edit button where it takes all the data from a datatable.net
EditWorker = function (TipDocument, N_Document, NomWorker, WorkWorker, WorkWorker, JobCode, JobNumber, OfficeOffice, JobWise, JobManager, JobDisplay) {... code meted previously ...}