1

me gustaria saber como podria hacer una resta entre las variables totalprice y total price2 y mostrarla en el label Lbtot

Aqui mi codigo :

                 $(function() {
                     $("input[type=checkbox]").change(function() {
                         var totalPrice = 0, ctlPrice;
                         $('.Gvcobranzas tr').each(function() {
                             if ($(this).find('input:checkbox').attr("checked")) {
                                 ctlPrice = $(this).find('[id$= Label3]');
                                 totalPrice += parseFloat(ctlPrice.text().replace(/[^\d\.]/g, ''));
                             }
                         });
                         $('.sum').text("$ " + totalPrice.toFixed(2));
                     });
                 });

                 $(function() {

                     $("input[type=checkbox]").change(function() {
                         var totalPrice2 = 0, ctlPrice2;
                         $('.GvNotasCredito2 tr').each(function() {
                             if ($(this).find('input:checkbox').attr("checked")) {
                                 ctlPrice2 = $(this).find('[id$= Label20]');
                                 totalPrice2 += parseFloat(ctlPrice2.text().replace(/[^\d\.]/g, ''));
                             }
                         });
                         $('.res').text("$ " + totalPrice2.toFixed(2));
                     });
                 });

             }

2 respuestas 2

1

Dos detalles:

  • Cuando usas $(function () { //mi codigo}) lo que estás haciendo es ejecutar el código cuando la página esté cargada y lista. con lo que lo ideal es que todo el código esté dentro de la misma función.

  • De todo modos, el tener dos funciones, una para obtener cada valor, es una buena idea, pero lo ideal sería llamar a cada una y que éstas devolviesen el valor que calculan.

Si unes estas dos ideas puedes hacer lo siguiente:

$(function() {

    function getTotalPrice() {
        var totalPrice=0;
        $('.Gvcobranzas tr').each(function() {
            if ($(this).find('input:checkbox').attr("checked")) {
                var ctlPrice = $(this).find('[id$= Label3]');
                totalPrice += parseFloat(ctlPrice.text().replace(/[^\d\.]/g, ''));
            }
        });
        return totalPrice;
    }

    function getTotalPrice2() {
        var totalPrice2=0;
        $('.GvNotasCredito2 tr').each(function() {
            if ($(this).find('input:checkbox').attr("checked")) {
                var ctlPrice2 = $(this).find('[id$= Label20]');
                totalPrice2 += parseFloat(ctlPrice2.text().replace(/[^\d\.]/g, ''));
            }
        });
    }

    var totalPrice=getTotalPrice();
    var totalPrice2=getTotalPrice2();
    $('.sum').text("$ " + totalPrice.toFixed(2));
    $('.res').text("$ " + totalPrice2.toFixed(2));

    var difference=totalPrice - totalPrice2;
    $('#LbTot').text("$ "+difference.toFixed(2));
});
2
  • Si quisiera en vez d emostrarlo en un label como podria hacerlo con un texbot? es igual?
    – otroklk8
    el 28 ago. 2017 a las 16:46
  • Sí, prácticamente puedes añadir el texto en cualquier elemento con ".text()" o ". val()" el 29 ago. 2017 a las 14:39
0

Encierra toda la logica en solo metodo anonimo. Luego las variables totalPrice y totalPrice2 las declaras fuera de los eventos change para que ambos eventos puedan acceder las variables. Creas un boton que al darle clic, reste los 2 valores de las variables. Recuerda crear el evento dentro de la funcion anonima tambien para que pueda acceder las variables totalPrice y totalPrice2:

$(function() {

    var totalPrice = 0;
    var totalPrice2 = 0;

     $("input[type=checkbox]").change(function() {
         var ctlPrice;
         totalPrice = 0;
         $('.Gvcobranzas tr').each(function() {
             if ($(this).find('input:checkbox').attr("checked")) {
                 ctlPrice = $(this).find('[id$= Label3]');
                 totalPrice += parseFloat(ctlPrice.text().replace(/[^\d\.]/g, ''));
             }
         });
         $('.sum').text("$ " + totalPrice.toFixed(2));
     });

     $("input[type=checkbox]").change(function() {
        var ctlPrice2;
        totalPrice2 = 0;
         $('.GvNotasCredito2 tr').each(function() {
             if ($(this).find('input:checkbox').attr("checked")) {
                 ctlPrice2 = $(this).find('[id$= Label20]');
                 totalPrice2 += parseFloat(ctlPrice2.text().replace(/[^\d\.]/g, ''));
             }
         });
         $('.res').text("$ " + totalPrice2.toFixed(2));
     });

     $("#restar").click(function() {
         alert(totalPrice - totalPrice2);// imprimes la resta
     });

 });

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.