根据输入值更改div内容

时间:2014-09-22 17:48:49

标签: javascript jquery

我有一个带有一些数据的json文件。我尝试做的是获取输入值,然后当它与折扣规则之一匹配时,价格字段应更改为适当的价格。

我的JSON:

{
  "product": {

    "discounts": {

      "10": {
        "id": "151203",
        "quantity": "10",
        "price": {
            "price": 5.35

        },
        "percentage": 0.05,
        "unit": false
      },

      "50": {
        "id": "151204",
        "quantity": "50",
        "price": {
            "price": 4.95

        },
        "percentage": 0.12,
        "unit": false
      }

    }

  }
} //etc

我的Html

<div class="quantity" id="wqs-quantity">
  <input type="text" name="quantity" value="1">                                         
 </div>

到目前为止我的jquery:

var qty = $('#wqs-quantity input').val();
$('#wqs-quantity input').change(function(e){
 if(qty == discount.quantity){
   $('.wqs-price .item-price').text(discountPrice);
 }
});

这段代码在getJSON调用中。

现在价格区域总是变为4.95。所以我尝试的是当有人填写10或50时,价格变为€5.35€4.95 ......

有人知道我做错了什么吗?

3 个答案:

答案 0 :(得分:1)

1)你错过了一个“打折”的开头:{[第3行]

2)if-statement是否在循环中通过所有可能的“折扣”?

尝试使用像这样的循环(其中值p是JSON对象):

    $('#wqs-quantity input').change(function(e){
      var qty = $(this).val();
      var d = p.product.discounts;

      for (var key in d) {
        if (d.hasOwnProperty(key)) {
          if (qty == d.key.quantity) {
            $('.wqs-price .item-price').text(discountPrice);
          }
        }
      }

    });
3)如果数量至少是折扣中的数量,则必须应用折扣(因此,数量10的折扣为5%,50以上的折扣为12%)。然后你可以使用这个循环(检查每个数量阈值并保存最后一个适用的数量):
    var p = JSON.parse(data);

    $('#wqs-quantity input').change(function(e){
      // get new quantity
      var qty = $(this).val();

      // get discount thresholds
      var d = p.product.discounts;

      // the current value to fall back on
      var new_price = $('.wqs-price .item-price').text();

      // loop through all discounts
      for (var key in d) {
        if (d.hasOwnProperty(key)) {
          var diff = (qty-d[key].quantity);

          // check if current quantity is above (or the same as) the threshold
          if (diff >= 0) {
            new_price = d[key].price.price;
          }
        }
      }

      $('.wqs-price .item-price').text(new_price);

    });
(为此,不同的“折扣”应从最低数量阈值到最高数量进行排序。)

4)为什么在“价格”内使用对象“价格”? (例如“discounts.10.price.price”而不是“discounts.10.price”)

工作示例:http://jsfiddle.net/PhilQ/ga59vbx3/11

答案 1 :(得分:0)

获取更改事件中的输入值:

if($(this).val() == discount.quantity){
    ...
}

否则您将始终retrieve the initial value

答案 2 :(得分:0)

qty侦听器被定义时,您绑定.change()的值,因此无论数量是否发生变化,这都是它始终认为存在的数量。

您希望将该分配移动到函数中:

// clean up the data format first into the discounts array:
var data = JSON.parse(json_data);
var discounts = [], tmp;
for (var key in data.product.discounts) {
    if (data.product.discounts.hasOwnProperty(key)) {
        tmp = data.product.discounts[key];
        tmp.quantity = Number(tmp.quantity);
        tmp.price = tmp.price.price;
        discounts.push(tmp);
    }
}
discounts.sort(function (x, y) { 
    return x.quantity - y.quantity;
});

// now for the actual logic:
$('#wqs-quantity input').change(function(e) {
    var price, qty = this.val();
    discounts.forEach(function (disc) {
        if (qty >= disc.quantity) {
            price = disc.price;
        }
    });
    if (price !== undefined) {
        $('.wqs-price .item-price').text(price);
    }
});

好的,这比你的简单案例要多得多。首先要注意的是,我基本上将您的JSON转换为其他JSON,这使得问题变得更加容易。

之后,我们遍历dicsounts,如果数量大于折扣数量,我们设置一个名为&#34; price&#34;的函数局部变量。到折扣的价格。如果设定了该价格(因此未定义),我们会将商品价格更改为该价格。

函数内的this.val()是关键;这表示我们将在每个给定的循环中取值,而不是在开始时取一次。

相关问题