Remove the disabled to an html tag with jquery

0

what I want to do is that depending on the result of my function loadOptions a label is enabled but only when the select is loaded, if it is not enabled but I do not know how to do the condition the problem is punctual I just need to know how to raise the if () {} correctly.

let cargarOpciones = (select, idSelect, valorCard, valorEstados, valorDistrito, valorProvincia ) => {

     $.ajax({
            type:'POST',
            url: 'cargar_estados.php',
            data:{
                    id:idSelect,
                    valorCard:valorCard,
                    valorEdo:valorEstados,
                    valorDistrito:valorDistrito,
                    valorProvincia:valorProvincia
            },
            dataType:'json'
        })
        .done(function(lista_rep1){
          
            select.empty();
            select.append('<option selected disabled value="0">Seleccione...</option>');
            for (var i = 0; i < lista_rep1.length; i++) {
                select.append('<option value="'+lista_rep1[i][idSelect]+'">'+lista_rep1[i][idSelect]+'</option>');
            };
        })
        .fail(function(){
            alert('error al cargar las listas');
        });
    }
	if( cargarOpciones == true){
   selectProvincia.prop( "disabled", false );
   }
    
asked by javier 12.12.2018 в 20:25
source

2 answers

0

Check from your result list

let esVacio = lista_rep1.length == 0;
select.empty();
select.prop('disabled',esVacio);
select.append('<option selected value="0">Seleccione...</option>');

And to disable it use the prop () function of jquery

    
answered by 12.12.2018 в 20:35
0

$. ajax () asynchronous method

The main problem that occurs in your code (among others) is that $.ajax() is an asynchronous method, so we must wait until it is completed in order to evaluate its result, in this case we have to use setTimeout() or setInterval() to evaluate the result from time to time.

The following code can help you determine when $.ajax() already ended:

In your case modify the code according to the following example:

let cargarOpciones = (select, idSelect, valorCard, valorEstados, valorDistrito, valorProvincia ) => {
     ajax_done = false;
     $.ajax({
            type:'POST',
            url: 'cargar_estados.php',
            data:{
                    id:idSelect,
                    valorCard:valorCard,
                    valorEdo:valorEstados,
                    valorDistrito:valorDistrito,
                    valorProvincia:valorProvincia
            },
            dataType:'json'

     }).done(function(lista_rep1){
            select.empty();
            select.append('<option selected disabled value="0">Seleccione...</option>');
            for (var i = 0; i < lista_rep1.length; i++) {
                select.append('<option value="'+lista_rep1[i][idSelect]+'">'+lista_rep1[i][idSelect]+'</option>');
            };
            ajax_done = true;

     }).fail(function(){
            alert('error al cargar las listas');
            ajax_done = 2;
     });
};
cargarOpciones(select, idSelect, valorCard, valorEstados, valorDistrito, valorProvincia );

waiting_for_AJAX = setInterval(function() {
  if (ajax_done) {
    clearTimeout(waiting_for_AJAX);
    if(ajax_done==true)
      selectProvincia.prop( "disabled", false );
  };
}, 500);

Consider rethinking your code because although this solution works, it is still an orthodox method.

In the following example I leave you a routine that executes a callBak() , which in your case would be the correct way to reframe the problem:

var A = () => {
 alert("hello word!");
};
var B = (callback) => {
 callback();
};
B(A);

If we extrapolate the example to your code, it would look like this:

let cargarOpciones = (select, idSelect, valorCard, valorEstados, valorDistrito, valorProvincia, callback ) => {

     $.ajax({
            type:'POST',
            url: 'cargar_estados.php',
            data:{
                    id:idSelect,
                    valorCard:valorCard,
                    valorEdo:valorEstados,
                    valorDistrito:valorDistrito,
                    valorProvincia:valorProvincia
            },
            dataType:'json'

     }).done(function(lista_rep1){
            select.empty();
            select.append('<option selected disabled value="0">Seleccione...</option>');
            for (var i = 0; i < lista_rep1.length; i++) {
                select.append('<option value="'+lista_rep1[i][idSelect]+'">'+lista_rep1[i][idSelect]+'</option>');
            };
            callback();

     }).fail(function(){
            alert('error al cargar las listas');
     });
};
cargarOpciones(select, idSelect, valorCard, valorEstados, valorDistrito, valorProvincia, function() { selectProvincia.prop( "disabled", false ) });

I hope this helps you ...;)

    
answered by 13.12.2018 в 00:15