简体   繁体   English

更改选择框时计算价格变化

[英]Calculating price change when changing select box

I'm sure this as been ask before but I am unable to find an answer. 我确定之前已经问过,但我无法找到答案。

What i'm trying to do: 我想做什么:

  • Update a price field <p class="price">&pound;<span>50.00</span></p> when a user changes a select box 当用户更改选择框时,更新价格字段<p class="price">&pound;<span>50.00</span></p>
  • Have multiple select boxes that work together on updating the price correctly 有多个选择框可以正常更新价格
  • reset the price if all select boxes are at original values 如果所有选择框均为原始值,则重置价格

NOTE: I am not very flexible with changing the HTML layout. 注意:我对更改HTML布局不是很灵活。

Heres what I have so far: 以下是我到目前为止的情况:

HTML HTML

<p class="price">&pound;<span>50.00</span></p>

<select name="s1">
    <option value="1" data-price="0">Item 1</option>
    <option value="2" data-price="10.10">Item 2 + &pound;10.10</option>
    <option value="3" data-price="20.10">Item 3 + &pound;20.10</option>
    <option value="4" data-price="30.10">Item 4 + &pound;30.10</option>
</select>

<select name="s2">
    <option value="1" data-price="0">Item 1</option>
    <option value="2" data-price="10.10">Item 2 + &pound;10.10</option>
    <option value="3" data-price="20.10">Item 3 + &pound;20.10</option>
    <option value="4" data-price="30.10">Item 4 + &pound;30.10</option>
</select>

JQuery JQuery的

var price = $('.price span').text();
$('.price').attr({'data-current': price, 'data-price': price})

$('select').change(function(){
    var $price = $('.price span');
    //var originPrice = parseFloat($('.price').attr('data-price'));
    var currentPrice = parseFloat($('.price').attr('data-current'));
    var addPrice = parseFloat($(this).children('option[value='+$(this).val()+']').attr('data-price'));
    var newPrice = '';

    if(isNaN(addPrice) || addPrice == 0){
        //reset
        var reset = $('.price').attr('data-price');
        $price.text(reset)
        $('.price').attr('data-current', reset)
    } 
    else {
        newPrice = currentPrice + addPrice;
        $price.text(parseFloat(newPrice).toFixed(2))
        $('.price').attr('data-current', newPrice)
    }

});

Its looking quite messy at the moment, I have created this as a test from the default Eshop (Wordpress) product page layout. 它目前看起来相当混乱,我已经将其创建为默认Eshop(Wordpress)产品页面布局的测试。 Eshop displays the price and select boxes similar to: Eshop显示价格并选择类似于的框:

<option value="2" data-price="10.10">Item 2 + &pound;10.10</option>

I have added all data values, currently my example is not full working as select boxes override each other and i'm not sure how to fix it. 我已经添加了所有data值,目前我的示例并不完整,因为选择框会互相覆盖,我不知道如何解决它。 And ideas? 和想法?

Here is the JsFiddle 这是JsFiddle

Thanks. 谢谢。

Assuming I've understood the question correctly, you want to add the values selected in the select elements to the initial price. 假设我已正确理解了这个问题,您希望将select元素中select的值添加到初始价格中。 If that's the case you can massively simplify your code. 如果是这种情况,您可以大规模简化代码。 Try this: 尝试这个:

$('.price-option').change(function(){
    var price = parseFloat($('.price').data('base-price'));

    $('.price-option').each(function(i, el) {
        price += parseFloat($('option:selected', el).data('price'));
    });

    $('.price span').text(price.toFixed(2));
}); 

Example fiddle 示例小提琴

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

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