根据输入值更改div内容

问题描述:

我有一些带有某些数据的json文件。我试图做的是获得输入值,然后当它与折扣规则中的一个相匹配时,价格字段应该改变为合适的价格。根据输入值更改div内容

我的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的pricefield更改为€4.95 5.35€罢了......

有谁知道我在做什么错?

+0

看来你的JSON格式不正确。在折扣和产品折扣之前缺少报价。我不知道你是否错误地复制了它,或者如果这是问题所在。 – Mic1780 2014-09-22 17:56:25

1)你错过了 “折价” 的开口:{[3号线]

2)通过所有可能的 '折扣' 一个循环内的if语句?

尝试使用循环像这样的(其中,值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)如果折扣具有当量是至少在折扣的量要施加(以便5% 10折优惠,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

+0

你是什么意思与“哪里值p是JSON对象”?你有没有例子? – Meules 2014-09-22 19:10:22

+0

问题中的JSON数据对象。 像这样: var p = { “product”:{.... etc ... }; – Philip 2014-09-22 19:14:26

+0

我用你的代码在这里做了一个小提琴...我不明白:(看我的小提琴[链接](http://jsfiddle.net/meules/ga59vbx3/) – Meules 2014-09-22 19:39:16

获取变化事件中输入值:

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

否则你永远retrieve the initial value

你要绑定的qty值一旦当.change()监听器被定义,所以这是它一直认为存在,无论数量是否是数量茶nges。

你,而不是想那项任务移动到功能:

// 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,如果数量比折量大的情况下,我们制定了一个函数局部变量称为“价格”,以折扣的价格。如果该价格被设置(因此不会保持未定义),那么我们将项目价格更改为该值。功能里面的this.val()是关键的东西;那就是说我们要在每一个给定的循环中取得价值,而不是在开始时考虑一次。