【发布时间】:2015-10-01 12:37:45
【问题描述】:
我有一系列带有价格值的按钮。我想从 s span.quote-price 中的总值中添加/减去按钮值。
下面的代码运行良好,并且总计价格,但如果可能,我想对其进行调整,以便如果未选择按钮或没有选择类别,则不会添加该数字。
目前,即使取消选择按钮,价格也会不断增加。谢谢你的帮助。
var theTotal = 0;
$('button').click(function() {
theTotal = Number(theTotal) + Number($(this).val());
$('span.quote-price').text("£" + theTotal.toFixed(2));
});
$('select').click(function() {
theTotal = Number(theTotal) + Number($(this).val());
$('span.quote-price').text("£" + theTotal.toFixed(2));
});
$('button#reset').click(function() {
$('span.quote-price').html("£0.00");
});
$('span.quote-price').text("£" + theTotal.toFixed(2));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="package_1" value="10.00" name="package_1">Package 1</button>
<button id="package_2" value="20.00" name="package_1">Package 2</button>
<button id="package_3" value="30.00" name="package_1">Package 3</button>
<p><span class="quote-price">0.00</span>
</p>
【问题讨论】:
-
您能否展示您的 HTML 和/或工作示例?
-
使用复选框代替按钮是否适合您的目的?
-
不幸的是,我需要按钮,用于样式设置。我已经更新了 html - 感谢您的帮助。
标签: javascript jquery function var