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
@Sergio This returns me the correct values from which I return my external file.
– Matheus Portela
Matheus, put the result here.
– Sergio
@Sergio
Object {adicionado: "1", id_produto: "13"}
– Matheus Portela
Thank you. There are more elements with this ID
#cartao_adicionado
or only 1 on the page?– Sergio
@Sergio Only one on the page.
– Matheus Portela
The inputs you refer to, which you want to write in the Success, are
type="text"
ortype="hidden"
?– Sergio
@Sergio No sucess, quero
type="hidden"
, but I leave astype=text
to know what value is being assigned to it.– Matheus Portela
If you do it directly on the console
$j('#cartao_adicionado').val('so-um-teste');
the value changes?– Sergio
@Sergio The displayed value changes, but inspecting the element, the value remains 0.
– Matheus Portela
And when you use as you have in the question the value also changes but the inspected does not change that is?
– Sergio
@Sergio That would be right.
– Matheus Portela
In this case I think you’re mixing concepts. Take a look here: https://answall.com/a/208023/129
– Sergio
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.– Sergio
@So Sergio, how could I correct this issue?
– Matheus Portela
You mean the question here or the code in your project?
– Sergio
Your project is all right. The ajax value is being input.
– Sergio
Let’s go continue this discussion in chat.
– Sergio
@Sergio Alright.
– Matheus Portela