0

我做这个功能:

function price(val){
var test = $('span.hikashop_product_price_full:visible').text();
var test2 = test.split('%');
var test3 = test2[1].split(' ');
var test4 = test3[0].replace(',','.');
var eyy = parseFloat(test4) *val/100 + parseFloat(test4);
eyy = eyy.toFixed(2)
var ett = test.replace(test3[0]+' '+test3[1],'<span class=\"hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount\">'+eyy+' '+test3[1]+'</span>');
$('span.hikashop_product_price_full:visible').html(ett);

}

我从动态<span>标签中获取一个值(说它是动态的,因为它会根据其他字段而变化)。我有一个领域

<select id="custome_vara" name="data[item][custome_vara]" size="1" onchange="price(this.value);">
<option value="" id="custome_vara_" selected="selected">alb</option>
<option value="20" id="custome_vara_20">stejar auriu</option>
<option value="30" id="custome_vara_30">moreiche</option>
</select>

每次我调用它都会触发。我只需要运行一次即可添加%到初始价格。

跨度看起来像这样

<span class="hikashop_product_discount">-10%</span><span class="hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount">51,34 €&lt;/span> </span> 

问题:

当我调用函数 price() 时,它会在此函数上运行选择并将选择设为 int。如何使选择成为静态,并且字段选择/选项在此静态选择中添加一个百分比,每个选项仅一次。

我希望你明白。

对不起我的英语不好。

谢谢。

4

1 回答 1

0

改变

<select id="custome_vara" name="data[item][custome_vara]" size="1" onchange="price(this.value);">

<select id="custome_vara" name="data[item][custome_vara]" size="1">

然后添加:

$('#custome_vara').one('change',function(){
    price($(this+'option:selected').val());
});

编辑:为了使您的意图明确,我将在此处发布一些“错误”:

首先,您缺少以下标记示例:

'span.hikashop_product_price_full:visible'

第二:

eyy = eyy.toFixed(2)

应该:

eyy = eyy.toFixed(2);

第三:

'<span class=\"hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount\">'

可以写成:

'<span class="hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount">'

我创建了这个小提琴页面来帮助我们互相理解:http: //jsfiddle.net/2fxxD/

EDIT2:我想我明白你想要什么。在此处查看以下操作:http: //jsfiddle.net/2fxxD/3/

function priceNew(val) {
    var discountedPrice = $('.hikashop_product_price_with_discount');
    var percentOff = $('.hikashop_product_discount');
    var price = discountedPrice.text().split(' ');
    var decimalPrice = price[0].replace(',', '.');
    var eyy = parseFloat(decimalPrice) * val / 100 + parseFloat(decimalPrice);
    eyy = eyy.toFixed(2);
    var newPrice = eyy + ' ' + price[1];
    discountedPrice.text(newPrice);
    percentOff.text('-' + val + '%');
    $('.hikashop_product_price').text(newPrice);
}

$('#custome_vara').one('change', function() {
    priceNew($(this + 'option:selected').val());
});

标记:

<select id="custome_vara" name="data[item][custome_vara]" size="1">
    <option value="" id="custome_vara_" selected="selected">alb</option> 
    <option value="20" id="custome_vara_20">stejar auriu</option>
    <option value="30" id="custome_vara_30">moreiche</option>
</select>
<div>
<span class="hikashop_product_price_full" id="hikashop_product_price_full"> 
    <span class="hikashop_product_discount">-10%</span>
    <span class="hikashop_product_price hikashop_product_price_0 hikashop_product_price_with_discount">51,34 €&lt;/span> 
    </span>
</div>
于 2012-04-10T11:57:21.113 回答