简体   繁体   English

jQuery 价格计算器

[英]jQuery price calculator

I am trying to create a simple jQuery calculator but it doesn't seem to be working as I hope it to be.我正在尝试创建一个简单的 jQuery 计算器,但它似乎并没有像我希望的那样工作。 Basically it should calculate automatically at 0 the price from the input boxes and then multiply when the +/- are clicked.基本上它应该在 0 时自动计算输入框中的价格,然后在点击 +/- 时相乘。

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/ http://jsfiddle.net/hktxyz5z/1/

I am not sure if I am over thinking, anyone got any ideas?我不确定我是不是想多了,有人有什么想法吗?

You need to give the variable declarations inside the click event:您需要在 click 事件中提供变量声明:

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.原因是,您在第一次加载时将值静态设置为0.00 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.解决方法是将以上两行代码放在.click()函数中。

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/工作小提琴: http : //jsfiddle.net/txk2d85y/

I'm not sure why you have two price boxes, but I've updated the fiddle to have 1, since this causes some unusual behaviour.我不确定为什么你有两个价格框,但我已经将小提琴更新为 1,因为这会导致一些不寻常的行为。

However, the root of the problem is that you have declared qty and price on document.ready, but then are not updating them whenever the buttons are clicked.但是,问题的根源在于您已在 document.ready 上声明了qtyprice ,但在单击按钮时并未更新它们。 Here is a fiddle demonstrating one solution:这是一个演示一个解决方案的小提琴:

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

However, I feel like you could make the whole thing much shorter and cleaner, reducing repetition of code between the plus and minus buttons.但是,我觉得您可以使整个过程更短更简洁,减少加号和减号按钮之间的代码重复。 Stay tuned for an update with a recommendation...请继续关注带有推荐的更新...

Update更新

Here is a refactored version:这是一个重构版本:

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

The changes I've made are:我所做的更改是:

  1. You don't need an input, since you are simple preventing the default action anyway.您不需要输入,因为无论如何您都可以简单地阻止默认操作。 In that case it is better to use a <button> , which I have done.在这种情况下,最好使用我已经完成的<button>
  2. Both quantity buttons now respond to the same event handler, which simple reads the adjustment attribute from the button and passes it to the adjustQty function.两个数量按钮现在响应相同的事件处理程序,它简单地从按钮读取调整属性并将其传递给adjustQty函数。
  3. The adjustQty function then calculates the new total, and updates the text boxes.然后adjustQty函数计算新的总数,并更新文本框。

If I've missed something obvious regarding the purpose of the second price box, please let me know and I'll adjust my example.如果我遗漏了与第二个价格框的用途有关的明显内容,请告诉我,我会调整我的示例。

Update 2:更新 2:

I'm guessing that you want to add up both price values, then multiply by the quantity?我猜您想将两个价格值相加,然后乘以数量? If so, this amended example should do the trick:如果是这样,这个修改后的例子应该可以解决问题:

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

The only real difference is that it calculates the total for both price boxes before it multiplies by the quantity.唯一真正的区别是它在乘以数量之前计算两个价格框的总和。

jQuery provide a plugin which may help you : "jQuery-Calx" jQuery 提供了一个可以帮助您的插件:“jQuery-Calx”

watch its demostration here :在这里观看它的演示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM