Jquery button with click counter

Asked

Viewed 404 times

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.

1 answer

-1

I decided only by modifying the variable value. Now it is called quantity and I was able to get its value with the following code var quantidade = $(this).closest('tr').find("#quantidade").text();

Browser other questions tagged

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