将输入值作为总和添加到另一个值
Add input value to another value as a total sum
我正在创建一个预算应用程序,用户可以在其中输入产品和价格。我希望它通过将每个输入值(HTML5 类型=数字(添加到下一个添加的行中的下一个数字来显示总成本。
片段:
$(document).ready(function() {
$('.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>");
$frm.parent().children(".messages").append(div);
$(".totalPrice").text("Total Price" + addAmount * addPrice);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="menu">
<h3>Shopping list</h3>
<div class="line">
<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">
我该怎么做?:)
谢谢
在函数外部声明一个全局变量
$(document).ready(function() {
var totalPrice=0;
$('.food').click(function() {
..
在计算总价时执行此操作
totalPrice = totalPrice + (addAmount * addPrice);
$(".totalPrice").text("Total Price" + totalPrice);
在
单击函数之外创建一个totalPrice
变量。然后在每次添加新项目时将addAmount * addPrice
添加到totalPrice
。然后在.totalPrice
div 中显示totalPrice
,而不是最后添加的项目的addAmount * addPrice
。
还希望在显示totalPrice
时添加对.toFixed()
的调用。调用 totalPrice.toFixed(2)
将确保仅显示两个小数位,以便它是货币值的正确格式。
工作示例:
$(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>");
$frm.parent().children(".messages").append(div);
totalPrice += addAmount * addPrice;
$(".totalPrice").text("Total Price: $" + totalPrice.toFixed(2));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="menu">
<h3>Shopping list</h3>
<div class="line">
<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添加另一个对象的高度作为边距
- 必应地图没有'当我向函数添加另一个参数时,t显示
- 对添加另一个选项卡的onsubmit函数停止重定向
- 单击链接时添加另一个选项
- 列表视图添加另一个不需要的页面项目
- 添加另一个变量后,正则表达式替换不起作用
- 如何通过单击按钮添加另一个复选框
- 添加另一个 JavaScript 时出现 JavaScript 问题
- iMacros 循环然后停止,然后添加另一个命令
- 如何向此 JavaScript 添加另一个条件
- 如何在Drupal 7中通过JS或Jquery模拟“添加另一个项目”
- 显示带有“添加另一个”按钮的选定结果的下拉列表
- 向嵌套的 JavaScript 对象添加另一个值
- 显示选择下拉列表中的结果,然后克隆并添加另一个选择
- 为什么 FirstPersonControls.js 在添加另一个事件侦听器时停止运行
- 在页面上放置一个随机生成器脚本 - 它可以工作.添加另一个具有不同名称的 - 不起作用
- 在Meteor TODO应用程序中向表单添加另一个字段
- 如何添加另一个"条目“;转换为此JSON
- 如何使用日期选择器添加另一个函数
- html:在锚点中添加另一个类