Problem changing the value of a textinput

1

I have the following function which I call both by keyboard and by a href simulating a button but the detail happens because when I execute it by keyboard it changes the value of ('#total') but when I execute it by clicking it does not .

function calcular_total(){

        $('#items tr').each(function () {
            var cantidad = $(this).find('td').eq(2).html();
            var precio = $(this).find('td').eq(3).html();
            if(typeof cantidad != 'undefined' && typeof precio != 'undefined')
            {
                var cantidad = $(this).find('td').eq(2).html();
                var precio2 = $(this).find('td').eq(3).html();
                var preciocorregido = parseFloat(precio2);
                var cantidadcorregido = parseInt(cantidad);
                var total1 = cantidadcorregido * preciocorregido;
                var total = parseFloat($('#totaloculto').val());
                total += parseFloat(total1);
                $('#totaloculto').val(total);
                $('#total').text(total);
            }
        });
    }; 

This way I call it by keyboard

$('#precio').keypress(function(e){
        if (e.which == 43){
            agregar_item();
            //e.preventDefault();
        };
    });

and in this way I call him by the

<a onClick="agregar_item(); return false;" class="btn-floating green btn-large"><i class="material-icons">add</i></a>

The function add_item () calls the function calculate total I have verified that it does everything except change the value of the inputtext '#total'

    
asked by jukson ismael villegas asuncio 11.08.2016 в 01:41
source

2 answers

1

Try launching the event like this:

<a onclick="return agregar_item();">...</a>

Or like this:

<a href="javascript:agregar_item();">...</a>

As a clarification, comment that the default behavior label <a> is first run the onclick , and then follow the href as long as the onclick does not return false .

You could also add the event to <a> via JavaScript:

var button = document.querySelector('tu_selector_para_a')
button.addEventListener('click', agregar_item, false)

Greetings and luck!

    
answered by 11.08.2016 в 07:51
1

In your code because you repeat twice the amount and price, try writing it like this:

function calcular_total(){

        $('#items tr').each(function () {
            var cantidad = $(this).find('td').eq(2).html();
            var precio = $(this).find('td').eq(3).html();
            if(typeof cantidad != 'undefined' && typeof precio != 'undefined')
            {
                var preciocorregido = parseFloat(precio);
                var cantidadcorregido = parseInt(cantidad);
                var total = parseFloat(cantidadcorregido * preciocorregido);
                total += parseFloat($('#totaloculto').val());
                $('#totaloculto').val(total);
                $('#total').text(total);
            }
        });
    };

Look at this answer: link

See this site too: link

    
answered by 10.09.2016 в 16:44