javascript中输入的总和
Total sum of an input in javascript
本文关键字:输入 javascript 更新时间:2023-09-26
我试图找到用户在弹出窗口中输入的价格的总和,并显示总数。我需要在输出下面再加一行吗?我正在尝试沿outMsg添加总数,但没有成功。感谢
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Array 02</title>
</head>
<form action="">
<p>BOOK SALE</p>
<p id="Books"</p>
<input type="button" value="Start" onClick="Books()";>
</form>
<body>
<script>
function Books()
{
//define variables
var arraySize = 2;
var bookName = new Array(arraySize);
var bookPrice = new Array(arraySize);
var total = 0;
var outMsg = "";
for(var i = 0; i < arraySize; i++)
{
bookName[i] = prompt('Enter Book Name: ', "");
bookPrice[i] = prompt('Enter the Price: ',0);
}
for(var i = 0; i < arraySize; i++)
{
outMsg = outMsg + bookName[i] + " " + bookPrice[i] + "</br>" + total; // put backslash n ('n) to show on window
//alert(outMsg)
document.getElementById("Books").innerHTML = outMsg;
}
}
</script>
</body>
<html>
在循环中,您需要对total
变量进行计数,如下所示:
for(var i = 0; i < arraySize; i++)
{
total += parseFloat(bookPrice[i]);
// Whatever else you need to do...
}
计算total
并使用parseInt
(或parseFloat
)键入
function Books() {
//define variables
var arraySize = 2;
var bookName = new Array(arraySize);
var bookPrice = new Array(arraySize);
var total = 0;
var outMsg = "";
for (var i = 0; i < arraySize; i++)
{
bookName[i] = prompt('Enter Book Name: ', "");
bookPrice[i] = prompt('Enter the Price: ', 0);
total += parseInt(bookPrice[i]);
}
for (var i = 0; i < arraySize; i++)
{
outMsg = outMsg + bookName[i] + " " + bookPrice[i] + "</br>" + total + "</br>"; // put backslash n ('n) to show on window
//alert(outMsg)
document.getElementById("Books").innerHTML = outMsg;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Array 02</title>
</head>
<form action="">
<p>BOOK SALE</p>
<p id="Books" </p>
<input type="button" value="Start" onClick="Books()" ;>
</form>
<body>
<script>
</script>
</body>
<html>
首先,您需要使用parseInt()
并将其添加到for循环中的total
变量中来计算合计。然后,您只需要计算要附加的响应,然后将第二个for循环之外的响应附加到所需的元素。阵列02
<form action="">
<p>BOOK SALE</p>
<p id="Books"</p>
<input type="button" value="Start" onClick="Books();" >
</form>
<body>
<script>
function Books()
{
//define variables
var arraySize = 2;
var bookName = new Array(arraySize);
var bookPrice = new Array(arraySize);
var total = 0;
var outMsg = "";
for(var i = 0; i < arraySize; i++)
{
bookName[i] = prompt('Enter Book Name: ', "");
bookPrice[i] = prompt('Enter the Price: ',0);
total += parseInt(bookPrice[i]);
}
console.info("TOTAL PRICE:" + total );//this will print in blue color the total price in your console
for(var i = 0; i < arraySize; i++)
{
outMsg = outMsg + bookName[i] + " " + bookPrice[i] + "</br>" + total; // put backslash n ('n) to show on window
//alert(outMsg)
console.info("OutMsg :" + outMsg);// use console.log(),console.info(), console.warn(), instead of the old school alert() along with the browser/firebug console.
//You should move this line out of for loop because you only have one element in which you wish to append your output
//document.getElementById("Books").innerHTML = outMsg;
}
document.getElementById("Books").innerHTML = outMsg;
}
</script>
</body>
<html>
相关文章:
- 如何在用户输入 javascript 时更改值
- 名称href标签根据用户文本输入JavaScript
- 赢得't输入javascript if/else语句
- 使用用户输入javascript进行权重转换
- 未捕获的语法错误:输入JavaScript意外结束
- 输入javascript函数
- 在无效输入:JavaScript 上停止注册
- 输入表单 浏览并获取文件夹路径并保存在文本输入Javascript中
- 用户输入 - Javascript Node js.
- 过滤文本输入 JavaScript
- 在单击“输入 JavaScript HTML 表单”时更改焦点
- 如何在客户端验证文件输入(Javascript)
- 如何获取输入Javascript的密码值
- 可以't使用CKEditor对话框输入javascript
- 如何在html代码中输入javascript变量
- 正在验证维度文本输入javascript
- 如何输入javascript对象's值转换为sql数据库
- 如何在用户输入javascript时删除字符
- 允许用户输入javascript代码
- 基于用户输入Javascript生成HTML