javascript - jQuery price calculator -


i trying create simple jquery calculator doesn't seem working hope be. should calculate automatically @ 0 price input boxes , multiply when +/- clicked.

it doesn't seem or show errors.

jquery(document).ready(function(){         var qty = parseint($('.qty').val());         var price = parsefloat($('#price').val());      $("#price").each(function(){       total += parsefloat(this.value);     }); 

http://jsfiddle.net/hktxyz5z/1/

i not sure if on thinking, got ideas?

you need give variable declarations inside click event:

var qty = parseint($('.qty').val());     var price = parsefloat($('#price').val()); 

the reason is, statically setting values 0.00 @ first load. every time increment or decrement, values not change. set once , not set when changed. don't dynamically change, after click events fired.

the solution put above 2 lines inside .click() functions.

jquery(document).ready(function(){    $("#price").each(function(){     total += parsefloat(this.value);   });    // button increment value   $('.qtyplus').click(function(e){     // stop acting button     e.preventdefault();     // field name     fieldname = $(this).attr('field');     // current value     var currentval = parseint($('input[name='+fieldname+']').val());     // if not undefined     if (!isnan(currentval)) {       // increment       $('input[name='+fieldname+']').val(currentval + 1);       qty = parseint($('.qty').val());           price = parsefloat($('#price').val());       $('#total').val((qty * price ? qty * price : 0).tofixed(2));     } else {       // otherwise put 0 there       $('input[name='+fieldname+']').val(0);       qty = parseint($('.qty').val());           price = parsefloat($('#price').val());       $('#total').val((qty * price ? qty * price : 0).tofixed(2));     }   });   // button decrement value till 0   $(".qtyminus").click(function(e) {     // stop acting button     e.preventdefault();     // field name     fieldname = $(this).attr('field');     // current value     var currentval = parseint($('input[name='+fieldname+']').val());     // if isn't undefined or greater 0     if (!isnan(currentval) && currentval > 0) {       // decrement 1       $('input[name='+fieldname+']').val(currentval - 1);       qty = parseint($('.qty').val());           price = parsefloat($('#price').val());       $('#total').val(((qty * price > 0) ? qty * price : 0).tofixed(2));     } else {       // otherwise put 0 there       $('input[name='+fieldname+']').val(0);       qty = parseint($('.qty').val());           price = parsefloat($('#price').val());       $('#total').val(((qty * price > 0) ? qty * price : 0).tofixed(2));     }   }); }); 

working fiddle: http://jsfiddle.net/txk2d85y/


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -