这一整天都在绞尽脑汁,我无法解决如何做到这一点......任何人都可以帮忙吗?
我想使用jQuery Calculation plugin从我网站上的输入框执行计算...
例如:
输入框 客人数量 和 份数
100 or less guests: £180
+
£1 per each additional guest (101+)
+
£0.60 per each additional guest serving
计算:将是180英镑+(额外客人x£1)+(总客人x份额@£0.60)
示例: 112位客人,2份服务费用为180英镑+ 12英镑+ 67.20英镑= 259.20英镑
任何人都可以提供帮助吗?
提前致谢
答案 0 :(得分:0)
不需要插件 - 简单的数学:
$('#numguests,#numservings').change(function() {
// get value of input - parse to int
var numguests = parseInt($('#numguests').val(), 10);
// base guest price
var guestprice = 180;
if (numguests > 100) {
// add cost of extra guests
guestprice = guestprice + (numguests - 100);
}
var servingsprice = 0;
// get value of input - parse to int
var numservings = parseInt($('#numservings').val(), 10);
if (numservings > 1) {
// add cost of extra servings
servingsprice = numguests * 0.60;
}
// round to 2 places and output
$('#total').html(parseFloat(guestprice + servingsprice).toFixed(2));
});
答案 1 :(得分:0)
最终代码:
<script type="text/javascript">
$(document).ready(function() {
$('#numguests,#numservings').change(function() {
var numguests = parseInt($('#numguests').val(), 10);
var guestprice = 180;
if (numguests > 100) {
guestprice = guestprice + (numguests - 100);
}
var servingsprice = 0;
var numservings = parseInt($('#numservings').val(), 10);
if (numservings > 1) {
servingsprice = numguests * 0.60 * (numservings-1);
}
$('#total').html(parseFloat(guestprice + servingsprice).toFixed(2));
});
});
</script>
<div id="calculator">
<p><strong>Use our Cost Calculator to work out a price:</strong></p>
<p>Number of guests <input id="numguests" value="100"><br>
Number of 4oz servings per guest<input id="numservings" value="1"><br>
Total price : &<span id="total"></span></p>
</div>
</div>