Assigning value to an input - jQuery

Asked

Viewed 790 times

1

I have two input type="hidden" from which I make an Ajax request to an external file in order to assign values to it. The request is returning me the value correctly, everything is right in it, but these values are not being assigned to input, for they keep the values from which it is placed in itself HTML even and I don’t know why this is happening.

Input code:

<input type="text" name="cartao_adicionado" id="cartao_adicionado" value="0" />
<input type="text" name="cartao_adicionado_product_id" id="cartao_adicionado_product_id"" value="0" />

Code of the Ajax request:

$j(document).ready(function(){
        $j.ajax({
            type: "POST",
            url: "verifica.php",
            data:{

            },
            dataType: 'json',
            cache: false,
            beforeSend: function(){

            },
            success: function(retorno){
                $j('#cartao_adicionado').val(retorno['adicionado']);
                $j('#cartao_adicionado_product_id').val(retorno['id_produto']);
            },
            complete: function(){

            },
            error: function(x,y,z){
                alert(x);
                alert(y);
                alert(z);
            }
        });
    })
  • What gives console.log(typeof retorno, retorno);?

  • @Sergio This returns me the correct values from which I return my external file.

  • Matheus, put the result here.

  • @Sergio Object {adicionado: "1", id_produto: "13"}

  • Thank you. There are more elements with this ID #cartao_adicionado or only 1 on the page?

  • @Sergio Only one on the page.

  • The inputs you refer to, which you want to write in the Success, are type="text" or type="hidden"?

  • @Sergio No sucess, quero type="hidden", but I leave as type=text to know what value is being assigned to it.

  • If you do it directly on the console $j('#cartao_adicionado').val('so-um-teste'); the value changes?

  • @Sergio The displayed value changes, but inspecting the element, the value remains 0.

  • And when you use as you have in the question the value also changes but the inspected does not change that is?

  • 1

    @Sergio That would be right.

  • In this case I think you’re mixing concepts. Take a look here: https://answall.com/a/208023/129

  • That is, the value changed as you want but the HTML attribute does not change because it is the value that you gave it at the beginning. That’s how it is and when you do it again $j('#cartao_adicionado').val(); after ajax you will see that the value is what you want.

  • @So Sergio, how could I correct this issue?

  • You mean the question here or the code in your project?

  • Your project is all right. The ajax value is being input.

  • @Sergio Alright.

Show 14 more comments
No answers

Browser other questions tagged

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