简体   繁体   English

如何在JS函数和循环中使用文本框用户输入的数据

[英]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.

相关问题 AngularJS + Bootstrap-验证文本框-仅在用户输入一些数据后如何显示验证反馈? - AngularJS + Bootstrap - Validating Text Box - how to show validation feedback only after the user has entered some data? 如何在模式中使用从 handleSubmit 函数返回的数据来显示用户输入的内容? - How do I use the data returned from a handleSubmit function in a modal to display what the user has entered? 复选框打勾时,如何将在一个文本框中输入的数据显示到另一个文本框中? - How display data entered in one text box to another text box on checkbox tick? 如果输入数据,则清空文本框 - Empty the text box if data's are entered 如何使用功能(for循环)从按钮获取用户输入以在文本框中显示? - How to get the user input from buttons to display in the text box using function (for-loop)? Javascript:如何使用循环功能在一个警报框中显示所有用户输入? - Javascript: How use a loop function to show all the user input in one single alert box? 如何从React js中的for循环中的多个文本框获取输入数据并将其传递给Api - How to get inputs data from multiple text box in for loop in React js and pass it to Api Rails:如何确认在文本框中输入的电子邮件地址与当前用户的电子邮件地址匹配? - Rails: How to confirm email address entered in text box matches current user's email address? DOM 清理 function 不是 escaping 用户输入的文本 - DOM sanitize function is not escaping the text that user entered DOM 清理 function 不是 escaping 用户输入的文本 - DOM sanitize function is not escaping the text that user entered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM