单击时从输入中删除数字

时间:2016-05-07 21:40:51

标签: javascript jquery

与此问题有关 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();
  });
});

2 个答案:

答案 0 :(得分:1)

从您要删除的行中获取价格和金额,然后从总计中减去它。

$(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 class='amount'>" + addAmount + "</p>", "<p class='product'> " + toAdd + " </p>", "<p class='price'>" + 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() {
    var subAmount = parseFloat($(this).siblings(".amount").text());
    var subPrice = parseFloat($(this).siblings(".price").text());
    totalPrice -= subAmount * subPrice;
    $(".totalPrice").text("Total Price: $" + totalPrice);

    $(this).closest("div").remove();
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<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>

我还为金额和价格元素分配了类,所以我不必在DIV中硬编码。

顺便说一句,你不应该在行中使用id='product',因为你要创建具有相同ID的多个元素。 ID应该是唯一的。我把它改成了一个班级。

答案 1 :(得分:0)

如何将每个添加的div的价格存储在其数据对象中?

var div = $("<div>");
div.data("price", addAmount * addPrice);

然后在删除事件中再次访问它:

$(document).on('click', '.delete', function() {
    var div = $(this).closest("div");
    totalPrice -= div.data("price");
    div.remove();
}