我需要创建一个表单,其中包含每种产品的价格 * 数量,然后需要将它们全部添加到总数中。 这是我到目前为止所拥有的。
$(document).ready(function(){
$('.bone-burger').on('keyup', '.quantity1', function() {
var price1 = +$(this).closest('.bone-burger').data('price');
var quantity1 = +$(this).val();
var amount1 = price1 * quantity1;
$('.smoke-burger').on('keyup', '.quantity2', function() {
var price2 = +$(this).closest('.smoke-burger').data('price');
var quantity2 = +$(this).val();
var amount2 = price2 * quantity2;
var realtotal = amount1 + amount2;
$('#total').text(realtotal);
});
});
});
这是一个 JSFiddle
https://jsfiddle.net/jamesleetaylor/z3b1dL9v/
我只尝试连接前两项。在 fiddle 中,在前两项中输入数字不会执行任何操作,但在我的网站(在 chromium 中)中,它们会起到一定的作用。
jamesleetaylor.com/voodoo
我进行了大量搜索以查找 JavaScript 表单的示例,但没有找到我需要的这个简单表单。
最佳答案
在提供的代码中,请注意您正在第一个监听器回调中添加第二个 keyUp 监听器。因此,如果您没有订购骨头汉堡(或更改骨头汉堡数量),您将无法订购烟熏汉堡。
重构代码
您创建的两个keyUp监听器执行相同的操作。它们使用具有相同结构并进行相同计算(使用不同数据)的兄弟元素。
您可以创建一个监听器来处理订单中的每一项。以此 HTML 代码为例。
<div class="form-group order-item" data-price='28'>
<label class="label-text" for="b1in">Bone Burger</label>
<input type="number" class="quantity" value='' placeholder="#" />
</div>
<div class="form-group order-item" data-price='29'>
<label class="label-text" for="b1in">Smoke Burger</label>
<input type="number" class="quantity" value='' placeholder="#" />
</div>
我向您拥有的不同 form-group
添加了一个名为 order-item 的类。然后,您为这两个(以及更多)项目创建一个监听器。
$('.order-item').on('keyup', function () {
updateTotal();
});
每次进行更改时,updateTotal 函数都会被调用。
编辑: jQuery 允许我们监听 change 事件而不是 onkeyup。这允许我们使用输入的向上和向下箭头。此代码可能更适合这种情况。
$('.order-item .quantity').change(function () {
updateTotal();
});
更新总数
现在,我们需要创建 updateTotal 函数。它基本上对每个价格 * 数量
求和可以这样完成。
function updateTotal() {
var total = 0;
$('.order-item').each(function () { // Loop through items, 'this' is the current element
var price = +$(this).data('price') || 0; // Retrieve the price
var quantity = +$(this).find('.quantity').first().val() || 0; // Retrieve the quantity
// The '|| 0' allows to set the variable to 0 if there is no price or quantity
total += price * quantity; // Add that to the total
});
// After the loop, we display the new total
$('#total').text(total);
}
$(document).ready(function() {
$('.order-item').on('keyup', function() {
updateTotal();
});
function updateTotal() {
var total = 0;
$('.order-item').each(function() {
var price = +$(this).data('price') || 0;
var quantity = +$(this).find('.quantity').first().val() || 0;
total += price * quantity;
});
$('#total').text(total);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
<div class="m js-modal">
<div class='modal-header'>
<h2 class='modal-title'>Order Form</h2>
</div>
<div class='modal-conten'>
<!-- <h4>Food</h4> -->
<div class="voodoo-form">
<!-- <button type="button" class="more btn btn-info">+</button> -->
<form>
<div class="form-group order-item" data-price='28'>
<label class="label-text" for="b1in">Bone Burger</label>
<div class="dollar">
<p class='euro'>28€</p>
<div class="spacer">
</div>
<input type="number" class="b1in quantity" value='' id="foodList" placeholder="#" />
</div>
</div>
<div class="form-group order-item" data-price='29'>
<label class="label-text" for="b2in">Smoke Burger</label>
<div class="dollar">
<p class='euro'>29€</p>
<div class="spacer">
</div>
<input type="number" class="b2in quantity" value='' id="foodList" placeholder="#" />
</div>
</div>
<div class="form-group order-item">
<label class="label-text" for="b3in">Voodoo Burger</label>
<div class="dollar">
<p class='euro'>30€</p>
<div class="spacer">
</div>
<input type="number" class="b3in quantity" value='' id="foodList" placeholder="#" />
</div>
</div>
<div class="form-group order-item">
<label class="label-text" for="d1in">Ayhuaska Sour</label>
<div class="dollar">
<p class='euro'>18€</p>
<div class="spacer">
</div>
<input type="number" class="d1in quantity" value='' id="foodList" placeholder="#" />
</div>
</div>
<div class="form-group order-item">
<label class="label-text" for="d2in">Beyond the Pale</label>
<div class="dollar">
<p class='euro'>10€</p>
<div class="spacer">
</div>
<input type="number" class="d2in quantity" value='' id="foodList" placeholder="#" />
</div>
</div>
<div class="form-group order-item">
<label class="label-text" for="d3in">Red</label>
<div class="dollar">
<p class='euro'>11€</p>
<div class="spacer">
</div>
<input type="number" class="d3in quantity" value='' id="foodList" placeholder="#" />
</div>
</div>
<div class="form-group order-item">
<label class="label-text" for="d4in">White</label>
<div class="dollar">
<p class='euro'>11€</p>
<div class="spacer">
</div>
<input type="number" class="d4in quantity" value='' id="foodList" placeholder="#" />
</div>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="submit-order btn btn-default">Place Order</button>
<p>Sub total: €<span id='subtotal'>0</span>
</p>
<p>Total: €<span id='total'>0</span>
</p>
</div>
</div>
关于javascript - 创建一个带有计算的简单 jQuery 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40558817/