0

这整天都在绞尽脑汁,我不知道该怎么做……有人能帮忙吗?

我想使用jQuery Calculation 插件从我网站上的输入框执行计算...

例如:

输入框: 客人人数和份数

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 英镑

任何人都可以提供帮助吗?

提前致谢

4

2 回答 2

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)); 
});​

这里的工作示例

于 2012-05-30T15:11:56.950 回答
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 : &amp;<span id="total"></span></p>
</div>




</div>
于 2012-05-31T12:21:05.977 回答