删除电话号码与此相关的问题 Remove one of the added lines on click从输入上点击
现在我想,当我删除行的总价格进行更新。我意识到我需要使用与我在总价中添加数字时所用的类似的东西,但我不知道如何执行此操作。
总之,当我添加一条线时,我希望将价格添加到总价中。当我删除它时,我希望从总价格中扣除已删除行上的数字。我需要一些帮助来了解如何从特定行中访问addPrice和addAmount。
HTML
<div id="menu">
<h3>Shopping list</h3>
<div class="line">
<p class="title">Amount</p>
<p class="title">Product</p>
<p class="title">Price</p>
<div>
<input class='amountInput' type='number' name='quantity' min='0' max='1000' step='1'>
<input class='productInput' type="text" name="message" value="">
<input class='priceInput' type='number' name='quantity' min='0' max='1000000' step='0.01'>
<button class="food">Add</button>
</div>
<div class="messages">
</div>
</div>
</div>
<div class="totalPrice">
</div>
jQuery的
$(document).ready(function() {
var totalPrice = 0;
$('.food').click(function() {
var $frm = $(this).parent();
var toAdd = $frm.children(".productInput").val();
var addPrice = $frm.children(".priceInput").val();
var addAmount = $frm.children(".amountInput").val();
var div = $("<div>");
div.append("<p>" + addAmount + "</p>", "<p id='product'> " + toAdd + " </p>", "<p>" + addPrice + "</p>", "<p class='delete'>" + "X" + "</p>");
$frm.parent().children(".messages").append(div);
totalPrice += addAmount * addPrice;
$(".totalPrice").text("Total Price: $" + totalPrice);
});
$(document).on('click', '.delete', function() {
$(this).closest("div").remove();
});
});
呀,谢谢:)我的错误 – Naomi
我没有完全阅读其他线程,但它看起来像您需要的值是它旁边的p元素,是否正确?你有工作样品吗? – yezzz
是的,这就是我需要的。 http://codepen.io/naomilea/pen/jqdydv – Naomi