jQuery price calculator

时间:2016-04-25 09:43:01

标签: javascript jquery html calculator

I am trying to create a simple jQuery calculator but it doesn't seem to be working as I hope it to be. Basically it should calculate automatically at 0 the price from the input boxes and then multiply when the +/- are clicked.

It doesn't seem to do anything or show any 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 am not sure if I am over thinking, anyone got any ideas?

3 个答案:

答案 0 :(得分:0)

You need to give the variable declarations inside the click event:

var qty = parseInt($('.qty').val());    
var price = parseFloat($('#price').val());

The reason is, you are statically setting the values to 0.00 at the first load. Every time you increment or decrement, the values do not change. It is set once and not set when it is changed. They don't dynamically change, after the click events are fired.

The solution is to put the above two lines inside the .click() functions.

jQuery(document).ready(function(){

  $("#price").each(function(){
    total += parseFloat(this.value);
  });

  // This button will increment the value
  $('.qtyplus').click(function(e){
    // Stop acting like a button
    e.preventDefault();
    // Get the field name
    fieldName = $(this).attr('field');
    // Get its current value
    var currentVal = parseInt($('input[name='+fieldName+']').val());
    // If is 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 a 0 there
      $('input[name='+fieldName+']').val(0);
      qty = parseInt($('.qty').val());    
      price = parseFloat($('#price').val());
      $('#total').val((qty * price ? qty * price : 0).toFixed(2));
    }
  });
  // This button will decrement the value till 0
  $(".qtyminus").click(function(e) {
    // Stop acting like a button
    e.preventDefault();
    // Get the field name
    fieldName = $(this).attr('field');
    // Get its current value
    var currentVal = parseInt($('input[name='+fieldName+']').val());
    // If it isn't undefined or its greater than 0
    if (!isNaN(currentVal) && currentVal > 0) {
      // Decrement one
      $('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 a 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/

答案 1 :(得分:0)

我不确定为什么你有两个价格框,但我已经更新了小提琴,因为这会导致一些不寻常的行为。

然而,问题的根源在于您已在document.ready上声明了qtyprice,但只要单击按钮就不会更新它们。这是一个展示一种解决方案的小提琴:

http://jsfiddle.net/hktxyz5z/2/

然而,我觉得你可以让整个事情变得更短更清洁,减少加号和减号按钮之间的重复代码。请继续关注推荐更新......

<强>更新

以下是重构版本:

http://jsfiddle.net/hktxyz5z/3/

我所做的改变是:

  1. 您不需要输入,因为您无论如何都很容易阻止默认操作。在这种情况下,最好使用我已经完成的<button>
  2. 两个数量按钮现在都响应同一个事件处理程序,该按钮从按钮读取调整属性并将其传递给adjustQty函数。
  3. 然后adjustQty函数计算新的总数,并更新文本框。
  4. 如果我错过了第二个价格框的目的,请告诉我,我会调整我的例子。

    更新2:

    我猜你想要加上两个价格值,然后乘以数量?如果是这样,这个修改过的例子应该可以解决问题:

    http://jsfiddle.net/hktxyz5z/4/

    唯一真正的区别是它在两个价格框乘以数量之前计算总价值。

答案 2 :(得分:0)

jQuery提供了一个可以帮助你的插件:“jQuery-Calx”

在这里观看它的演示:

 http://prototype.xsanisty.com/calx2/