Jquery button with click counter

0

Good morning. I have in my html a dynamic table with some data coming from the sql, among them an "available quantity" field and a button that with every click, make an ajax request 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 decremented. What I've done so far has been

$(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's value. but it only works the first time I click the button

    
asked by anonymous 14.05.2018 / 16:13

1 answer

0

I solved only by modifying the variable value. Now it is called quantity and I got the value of it with the following code var quantidade = $(this).closest('tr').find("#quantidade").text();

    
14.05.2018 / 19:37