I need to check if my JSON is empty to search for url with parameter set in Curl

Asked

Viewed 246 times

1

function BuscaMarca(marca, modelo, anomodelo) {
    marca = $('#marca').val();
    tipo = $('#tipo').val();
    $.get("curl.php?tabela=BuscaMarca&marca=" + marca + "&tipo=" + tipo, function(data, status) {
        if (data == 0) {
            alert("Empty");
        } else {
            $('#marca').html(data);
            BuscaModelo(); //Busca o Modelo 
        }

    });
}

I need that some way, mark and type are empty, it has to be table=Search&brand=&type=0

3 answers

1

Makes an Ternary IF when receiving the value of the field, asking if the content of the value is greater than 0. If it is not, it is 0.

IF Ternary

VARIÁVEL = (CONDIÇÃO) ? VERDADEIRO : FALSO

Code

function BuscaMarca(marca, modelo, anomodelo) {

    marca = ($('#marca').val().length > 0) ? $('#marca').val() : 0;
    tipo  = ($('#tipo').val().length > 0) ? $('#tipo').val() : 0;

    $.get("curl.php?tabela=BuscaMarca&marca=" + marca + "&tipo=" + tipo, function(data, status) {
        if (data == 0) {
            alert("Empty");
        } else {
            $('#marca').html(data);
            BuscaModelo(); //Busca o Modelo 
        }

    });
}

1

You can make an inline conditional to check if they are empty and assign the values accordingly, follow the example:

marca = ( $('#marca').val() != '' ? $('#marca').val() : '' );

Note: It does a check of the tag value, if it is different from empty, it executes what comes after the ? which is the value itself, if not it executes after the : which is the default value you specify.

That way the guy would be:

tipo = ( $('#tipo').val() != '' ? $('#tipo').val() : '0' );

1


You just need to validate the variables

tipo = $('#tipo').val();
if(tipo=="")tipo=0;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.