[英]How to use text box user entered data in a JS Function and loop
My code sort of works correctly but I want to tweak it and cannot seem to build the way I expect. 我的代码可以正常工作,但是我想对其进行调整,但似乎无法按照我的期望来构建。 Basically I have two labels and input.
基本上我有两个标签和输入。 Ask user to enter a certain number of weeks (in the code I have kept it at 3 but intend on going out longer) and number of days for each week entered supplement packs were taken.
要求用户输入一定的星期数(在代码中我将其保持为3,但打算延长时间),并输入了每周输入补充包的天数。 Button is clicked and a function is called.
单击按钮并调用一个函数。 I want the function to prompt the user to enter the number of packs for each day they were taken (if 2 weeks were entered and they only took supplements 3x a week I only want to iterate through the prompt 3 days for week-1 and 3 days for week-2).
我希望该功能提示用户输入每天服用的药盒数量(如果输入了2周,并且他们每周只服用3次补充剂,我只想在提示的第1天和第3天重复3天第2周的天数)。 As each day is entered the information is output back to the screen and a subtotal for each week is given.
每天输入时,信息将返回到屏幕,并给出每周的小计。 For a beginner I think I've learned quite a bit and mostly due to this site.
对于初学者,我认为我学到了很多东西,这主要归功于此站点。 Thanks in advance.
提前致谢。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Supplement Intake Log</title>
<link href="style.css" rel="stylesheet" type="text/css">
<!--JS Code here-->
<script>
function getPho() {
var week = document.getElementById("week").value;
var day = document.getElementById("day").value;
var numPacks = 0;
var subtotal = 0;
var count = 0;
var total = 0;
week = parseInt(week);
day = parseInt(day);
for(week = 1; week < 3; week++)
{
document.getElementById("output1").innerHTML="Week " + week + "
<br>";
count = 1;
subtotal = 0;
for(day = 1; day < 8; day++)
{
numPacks = parseInt(prompt("Enter the number of packs for
week " + week + " and day " + day + " . ", ""));
document.getElementById("output2").innerHTML+="Number of
packs for day " + day + " = " + numPacks + " supplement packs."+ "<br>";
subtotal = subtotal + numPacks;
count++;
}
total = total + subtotal;
document.getElementById("output3").innerHTML="Week " + week + "
subtotal is " + subtotal + " supplement packs." + "<br>";
}
document.getElementById("output4").innerHTML="The total for
these weeks is " + total + " supplment packs." + "<br>";
}
</script>
</head>
<body>
<header><h1>Weekly Supplement Intake Log</h1></header>
<section>
<label>Number of weeks on supplements:</label>
<input type="text" name="week" id="week" value=""><br>
<label>Number of days supplements taken each week:</label>
<input type="text" name="day" id="day" value="">
<div id="button">
<button type="button" id="calc" onClick="getPho()">Enter the number of
packs taken each day/week</button></div>
<div id="output1"></div>
<div id="output2"></div>
<div id= "output3"></div>
<div id="output4"></div>
</body>
</html>
This fiddle will loop through the weeks and for each week loop through the days per week taken and ask the user for an input of number of packs taken per day. 这个小提琴将遍历几周,而对于每周则遍历每周服用的天数,并要求用户输入每天服用的包装数。
var i=0;
while(i<numWeeks) {
var d=0;
while(d<daysPerWeek){
https://jsfiddle.net/auco824j/2/ https://jsfiddle.net/auco824j/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.