perform a single function jquery once?

1

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 ...}

    
asked by Nik.Code 01.08.2018 в 17:16
source

4 answers

3

I think what you're looking for is

$('#idBoton').one('click',llenarcombos);

So only the first time you press the button will work, after that jQuery will remove the listener from the button.

    
answered by 01.08.2018 в 17:26
0

you put the button to the listener so that when it is pressed, execute the example function ...

with the button Id you capture the event and thus trigger what you want ..

$("#id_boton").click(function(){
    $.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 hope you serve Bro ... ReNiceCode ..

    
answered by 01.08.2018 в 17:26
0

try this, this code will only load the script once when the document is ready

<script>
$( document ).ready(function() {
    llenarcombos();
});
</script>
    
answered by 01.08.2018 в 17:27
0

Automatic data loading, achieved by executing one function within another

function combo1(){
  $.ajax({
    success: function(r){
      var a=$("#combo1").val();
      combo2(a);
    }
  })
}
function combo2(a){
  $.ajax({
    success: function(r){
      var a=$("#combo2").val();
      combo3(a);
    }
  })
}
function combo3(a){
  $.ajax({
    success: function(r){

    }
  })
}
combo1();
<select id="combo1">
  <option value="1" onlclick="combo1()">dep1</option>
  <option value="2" onlclick="combo1()">dep2</option>
  <option value="3" onlclick="combo1()">dep3</option>
</select>

<select id="combo2">
  <option value="21" onlclick="combo2()">of1</option>
  <option value="22" onlclick="combo2()">of2</option>
  <option value="23" onlclick="combo2()">of3</option>
</select>
<select id="combo3">
</select>

But for each time you load the data, you must remove the previous ones ( <option> ) of the select that you will fill.

    
answered by 01.08.2018 в 17:47