选择选项时汇总属性值和文本

Sum up attribute value and text when option selected

我朋友网站上的许多产品都有不同的款式。根据您选择的价格,价格会发生变化。价格代码如下所示:

    <div id="WA_price">
        <p class="WA_price2">200&nbsp;€</p>
    </div>

当您选择变体时,代码如下所示:

<dd class="last">
    <div class="input-box">
        <select name="super_attribute[149]" id="attribute149" class="required-entry super-attribute-select">
            <option value="">Option wählen…</option>
            <option value="28" price="139" data-label="1000 mm">1000 mm -10&nbsp;€</option>
            <option value="30" price="149" data-label="1200 mm" class="selected">1200 mm</option>
            <option value="32" price="159" data-label="1400 mm">1400 mm +10&nbsp;€</option>
        </select>
    </div>
</dd>

我发现我必须将选项标签中的值添加到 <p class="WA_price2">200&nbsp;€</p> 中的 200 €。

我想不通的是如何获取价格值并将其添加到 p 中的价格。WA_price2。

我想您首先必须删除 &nbps;€,然后将数字作为值,将其添加到价格值中,然后再次将其插入 p-tag。

我只知道如何删除 &nbsp;€

jQuery(".WA_price2").each(function() {
    var $this = jQuery(this);
    $this.html($this.html().replace(/&nbsp;€/g, ''));
});

我真的需要一些帮助,但想不通。你可以找到我的代码片段 here.

您已经知道如何摆脱 &nbsp;€:

$this.html().replace(/&nbsp;€/g, '')

现在您需要将其解析为数字并将其添加到所选值:

$this.html((parseInt($this.html().replace(/&nbsp;€/g, '')) + parseInt(jQuery("#attribute149").val())) + "&nbsp;€");

我假设您需要将#attribute149 的值添加到内部文本的编号中。如果我错了,那么我恳请您创建一个 Fiddle 使用您当前拥有的内容以及用户可以使用预期结果执行的一些场景。

编辑:

JS 代码应该是这样的:

var priceContext = jQuery("#WA_price > .WA_price2")
var initialValue = parseInt(priceContext.html().replace(/&nbsp;€/g, ''));

jQuery("#attribute149").change(function() {
    priceContext.html(initialValue + parseInt(jQuery(this).find("option[value=" + jQuery(this).val() + "]").attr("price")) + "&nbsp;€");
});

参见Fiddle