2015-11-05 29 views
1

我有一些基本价格和额外价格的产品元素。目标是将额外的价格添加到基准价格,并用新计算的基准价格替换基准价格。jQuery Append不能与parseInt一起使用var

下面是我到目前为止的代码,console.logs给了我所有正确的反馈,但价格不会追加到.subtotal div,无论我尝试什么。

jQuery(document).ready(function() { 
 

 
    var extra = jQuery('.extra'); 
 
    extra.each(function() { 
 
    var $this = jQuery(this).html().replace(/[^\d,]/g, ''); 
 

 
    var subtotal_dest = jQuery(this).siblings('.subtotal'); 
 
    jQuery(subtotal_dest).css('border', '2px solid green'); 
 

 
    var subtotal = jQuery(this).siblings('.subtotal').html().replace(/[^\d,]/g, ''); 
 

 
    var newtotal = parseInt($this) + parseInt(subtotal); 
 
    var rendered_total = '€ ' + newtotal; 
 

 
    jQuery(rendered_total).appendTo(subtotal_dest); 
 

 
    console.log($this); 
 
    console.log(subtotal); 
 
    console.log(rendered_total); 
 
    console.log('-'); 
 
    }); 
 

 
});
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script> 
 
</head> 
 

 
<body> 
 

 
    <div class="product" style="margin-bottom:20px;"> 
 
    <!--subtotal should be €10 --> 
 
    <div class="subtotal">€ 5,00</div> 
 
    <div class="extra">€ 5,00</div> 
 
    </div> 
 
    <div class="product" style="margin-bottom:20px;"> 
 
    <!--subtotal should be €20--> 
 
    <div class="subtotal">€ 15,00</div> 
 
    <div class="extra">€ 5,00</div> 
 
    </div> 
 
    <div class="product" style="margin-bottom:20px;"> 
 
    <!--subtotal should be €30--> 
 
    <div class="subtotal">€ 25,00</div> 
 
    <div class="extra">€ 5,00</div> 
 
    </div> 
 

 
</body> 
 

 
</html>

+0

什么应该实现'jQuery(rendered_total)'? –

+0

只需在新计算的总数中添加欧元符号即可。 –

回答

1

$('€ ' + newtotal')没有任何用处。

更换

jQuery(rendered_total) 

jQuery("<div>").text(rendered_total). 

完整运行的代码:

jQuery(document).ready(function() { 
 

 
    var extra = jQuery('.extra'); 
 
    extra.each(function() { 
 
    var $this = jQuery(this).html().replace(/[^\d,]/g, ''); 
 

 
    var subtotal_dest = jQuery(this).siblings('.subtotal'); 
 
    jQuery(subtotal_dest).css('border', '2px solid green'); 
 

 
    var subtotal = jQuery(this).siblings('.subtotal').html().replace(/[^\d,]/g, ''); 
 

 
    var newtotal = parseInt($this) + parseInt(subtotal); 
 
    var rendered_total = '€ ' + newtotal; 
 

 
    jQuery("<div>").text(rendered_total).appendTo(subtotal_dest); 
 

 
    console.log($this); 
 
    console.log(subtotal); 
 
    console.log(rendered_total); 
 
    console.log('-'); 
 
    }); 
 

 
});
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <script type='text/javascript' src='https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js'></script> 
 
</head> 
 

 
<body> 
 

 
    <div class="product" style="margin-bottom:20px;"> 
 
    <!--subtotal should be €10 --> 
 
    <div class="subtotal">€ 5,00</div> 
 
    <div class="extra">€ 5,00</div> 
 
    </div> 
 
    <div class="product" style="margin-bottom:20px;"> 
 
    <!--subtotal should be €20--> 
 
    <div class="subtotal">€ 15,00</div> 
 
    <div class="extra">€ 5,00</div> 
 
    </div> 
 
    <div class="product" style="margin-bottom:20px;"> 
 
    <!--subtotal should be €30--> 
 
    <div class="subtotal">€ 25,00</div> 
 
    <div class="extra">€ 5,00</div> 
 
    </div> 
 

 
</body> 
 

 
</html>

+0

我明白了,谢谢。这工作!但我想知道为什么它显然必须是“文本”。我认为变量'rendered_total'已经是一个文本字符串了。 –

相关问题