关于这个问题
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();
  });
});

最佳答案

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



$(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应该是唯一的。我把它改成了一堂课。

10-07 13:33
查看更多