How to generate a condition in javascript to deny access to a function?

2

Well, I am trying to create a condition where if a select element is empty, deny the call to a function made in ajax, but my problem is that I do not know exactly how to create that condition. Here is my code attempt.

function LimpiaEstado() {

var Pais = document.getElementById('ddl_Perfil_Pais').value;

if (Pais = ''){
    ddl_Perfil_Estado.html('');
    //Aqui intento negar la funcion CatEstado en caso de que este vacio
}

 function CatEstado(Pais) {
            $.ajax({
                url: 'Talento.ashx?Tipo=Estados&Pais=' + Pais,
                dataType: 'JSON',
                type: 'POST',
                async: false,
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    $('#ddl_Perfil_Estado').html('');
                    $('#ddl_Perfil_Estado').append('<option value=""> Selecciona..</option>');

                    $.each(data.DatosCatalogoEstado, function (key, val) {
                        $('#ddl_Perfil_Estado').append('<option value="' + val.ID + '">' + val.ESTADO + '</option>');
                    })

                    $('#ddl_Perfil_Estado').selectpicker('refresh');
                },
                error: function (response) {
                    $('#ddl_Perfil_Estado').html('<option id="-1">none available</option>');
                }
            });
        }

If you know how to do it directly with Ajax, it would be equally helpful, or if you know how to do it, without necessarily being a condition. Any suggestion or advice is welcome.

    
asked by Guillermo Navarro 08.09.2016 в 02:07
source

2 answers

1

Why not check the condition of the function?

function CatEstado() {
    var Pais = document.getElementById('ddl_Perfil_Pais').value;
    if (Pais !== '') {
        $.ajax({
            //Acá va la llamada por AJAX
        });
    }
}
    
answered by 08.09.2016 / 06:57
source
3

Greetings if you do not want to change your code you can use the return false ;

if (Pais = ''){
    ddl_Perfil_Estado.html('');
    //Aqui intento negar la funcion CatEstado en caso de que este vacio
    return false;
}

This way you will avoid the call ajax, now on the other hand is the other option that Mariano presents to you.

if (Pais = ''){
    $.ajax({
        //Acá va la llamada por AJAX
    });
}
    
answered by 08.09.2016 в 07:02