html相关的源码:
<div class="goods_num clearfix">
<div class="num_name fl">数 量:</div>
<div class="num_add fl">
<input type="text" name="num" class="num_show fl" value="1">
<a href="javascript:;" class="add fr">+</a>
<a href="javascript:;" class="minus fr">-</a>
</div>
</div>
<div class="total">总价:<em id="price">{{ good.price }}</em>元</div>
相应的js部分:
<script>
$(function () {
//加号
var price1 = parseFloat($('#price').text());
var num = parseInt($('input[name="num"]').attr('value'));
$('.add').click(function(){
num++;
$('input[name="num"]').attr('value',num);
var total = num * price1;
$('#price').html(total.toFixed(2));
});
//减号
$('.minus').click(function () {
if(num>1){
num--;
$('input[name="num"]').attr('value',num);
console.log(num)
var total = num * price1;
$('#price').text(total.toFixed(2));
} });
});
</script>