0
Good morning. I have in my html a dynamic table with some data coming from sql, among them a field "available quantity" and a button that at each click, make a request ajax and decrease the amount available
"<tr>";
"<td>{$key2}</td>";
"<td data-nome={$value2} id='contador'>{$value2}</td>";
"<td ><input type='button' data-campo='{$key2}' data-modulo='{$p_arrPagina['idmodulo']}' data-tabela='{$p_idtabela}' class='addcampos btn btn-primary' value='Adicionar + 1'></td>";
"</tr>";
I need a jquery code to be able to show the user that the value is decreasing. What I have done so far is
$(function(){
$(document).on('click','.addcampos', function(e){
e.preventDefault();
var valor = $(this).closest('tr').find('td[data-nome]').data('nome');
var texto = $(this).closest('tr').find('#contador');
var tr = $(this).closest('tr');
var idmodulo = $(this).data('modulo');
var idtabela = $(this).data('tabela');
var campo = $(this).data('campo');
$.ajax({
url: 'localhost/controllers/recebido.php',
type: 'POST',
data: { 'idmodulo' : idmodulo,
'idtabela': idtabela,
'campo' : campo },
success: function(response){
valor--;
console.log(valor);
var novoTexto = texto.html(valor);
}
})
.done(function(dados) {
})
.fail(function() {
alert('ocorreu um erro');
})
});
I’m trying to decrease the variable value. but it only works the first time I click the button
I think the problem is being that the variable
novoTexto
does not update the element that the "value" variable refers to.– mutlei