Place Json value on variable

Asked

Viewed 1,817 times

2

What I’m doing wrong?

var GETEstado = $(this).val(json[0].GETEstado);
alert (GETEstado);

The Alert is exhibiting

[Object Object]

My complete code:

$(document).ready(function () {
    $(".transportadora").click(function () {
        id = $("input[type=radio][name='transportadora']:checked").val();
        estado = $("#estado2").val();
        $.getJSON("cotacoesBuscaTransportadora.php", {
            id_transportadora: id,
            estado: estado
        }, function (json) {
            $("#estadoT").val(json[0].estadoT);
            $("#valorCap").val(json[0].valorCap);
            $("#valorExcedCap").val(json[0].valorExcedCap);
            $("#valorAloremCap").val(json[0].valorAloremCap);
            $("#prazoCap").val(json[0].prazoCap);

            var GETEstado = JSON.stringify(json[0].GETEstado);
            var ResulteZero = JSON.stringify(json[0].ResulteZero);

        });
    });
});

jQuery(function ($) {
    if (GETEstado == "") {
        $.gritter.add({
            title: 'Erro',
            text: 'Preencha os dados do destinatário',
            class_name: 'gritter-error gritter-center'
        });
    }
    if (ResulteZero == 0) {
        $.gritter.add({
            title: 'Erro',
            text: 'Essa transportadora não entrega no estado de destino ou destino não cadastrado.',
            class_name: 'gritter-error gritter-center'
        });
    }
});
  • Using the JSON.stringify worked, what I’m not able to do is take the value of the variable to another function where will show a message on the screen. http://jsfiddle.net/zLqf3ccr/, see this link.

  • James, this is what you want: http://jsfiddle.net/zLqf3ccr/1/ ?

  • @Sergio, yes, but you need to be inside jQuery(function($) { otherwise does not load the message.

  • No, but you might have to jQuery in time of $: http://jsfiddle.net/zLqf3ccr/2/

  • @Sergio again, thank you very much!

2 answers

2

The $(this).val(valor) fills the field and returns the corresponding jQuery object, i.e., $(this). That’s why Alert shows this text object representation that you received.

If the field is being filled in correctly, just change your code a little:

var GETEstado = json[0].GETEstado;
$(this).val(GETEstado);
alert (GETEstado);
  • Wow, I didn’t even realize it, I went a completely different way. I think I’ll delete my answer. Hehe.

  • 1

    Wait a little longer his feedback, maybe we still need to parse JSON...

  • Using the JSON.stringify worked, what I’m not able to do is take the value of the variable to another function where will show a message on the screen. http://jsfiddle.net/zLqf3ccr/, see this link.

2


jQuery returns different things if used as getter or Setter.

If you use $(this).val(json[0].GETEstado) you’re using as a Setter and will return a jQuery object, not a number or string with value that was set.

If you use $(this).val()then will return the value of this.

If your Alert is to confirm that the value was set correctly you should do so:

$(this).val(json[0].GETEstado);
var GETEstado = $(this).val();
alert (GETEstado);

Besides, the problem you have in the code is asymchronism. The $.getJSON fetch the data and then need to run the second part of your code from its callback function. So:

$(document).ready(function () {
    $(".transportadora").click(function () {
        id = $("input[type=radio][name='transportadora']:checked").val();
        estado = $("#estado2").val();
        $.getJSON("cotacoesBuscaTransportadora.php", {
            id_transportadora: id,
            estado: estado
        }, function (json) {
            $("#estadoT").val(json[0].estadoT);
            $("#valorCap").val(json[0].valorCap);
            $("#valorExcedCap").val(json[0].valorExcedCap);
            $("#valorAloremCap").val(json[0].valorAloremCap);
            $("#prazoCap").val(json[0].prazoCap);

            var GETEstado = JSON.stringify(json[0].GETEstado);
            var ResulteZero = JSON.stringify(json[0].ResulteZero);
            //alert (GETEstado);
            next(GETEstado, ResulteZero);
        });
    });
});


function next(GETEstado, ResulteZero) {
    alert(GETEstado);
    if (GETEstado == "") {
        jQuery.gritter.add({
            title: 'Erro',
            text: 'Preencha os dados do destinatário',
            class_name: 'gritter-error gritter-center'
        });
    }
    if (ResulteZero == 0) {
        jQuery.gritter.add({
            title: 'Erro',
            text: 'Essa transportadora não entrega no estado de destino ou destino não cadastrado.',
            class_name: 'gritter-error gritter-center'
        });
    }
};
  • Using the JSON.stringify worked, what I’m not able to do is take the value of the variable to another function where will show a message on the screen. http://jsfiddle.net/zLqf3ccr/, see this link.

  • @James, this is what you want: http://jsfiddle.net/zLqf3ccr/1/ ?

Browser other questions tagged

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