简体   繁体   English

全局变量未从函数中更新

[英]Global variable is not getting updated from within a function

I am creating a simple form where user can set the hour. 我正在创建一个简单的表单,用户可以在其中设置小时数。 Through javascript validation it checks that there is a value in the form. 通过javascript验证,它检查表单中是否有值。 Originally the global variable "userInputHours" is set to 0. 最初,全局变量“ userInputHours”设置为0。

Within the function "validation()" when user fullfills the required criteria of filling in something in the form, the validation goes to "else" and should update global variable "userInputHours". 当用户完全填写表单中填写内容的必要条件时,在函数“ validation()”中,验证将转到“ else”,并应更新全局变量“ userInputHours”。

As a last step the HTML info-box should update the user input hours. 最后,HTML信息框应更新用户输入时间。

What is the problem? 问题是什么? I cannot get the user form input value to stay on the HTML info-box (it is visible for less than a second, then it gets overwritten by the original value "userInputHours"). 我无法让用户表单输入值停留在HTML信息框中(可见时间不到一秒钟,然后它被原始值“ userInputHours”覆盖)。

I know some answer would indicate I should use AJAX but that is to solve that only parts of the page is being reloaded. 我知道一些答案将表明我应该使用AJAX,但这是为了解决仅部分页面被重新加载的问题。 Using AJAX does not solve the base of my problem. 使用AJAX不能解决我的问题基础。

Scenarios and what works/don't work: 方案和有效/无效:


Scenario-1: Enter digits in form field "hours" and press button "submit-1": 方案1:在“小时”表单字段中输入数字,然后按“提交1”按钮:


Works: Console log prints "You added: [the number you added in HTML form]. 作品:控制台日志打印“您添加:[您以HTML格式添加的数字]。

Does not work: Running a console.log(userInputHours) from console itself returns the originally value [0] not the user added value. 不起作用:从控制台本身运行console.log(userInputHours)会返回原始值[0],而不是用户添加的值。 Note! 注意! For a very short moment, less than a second you actually see the user value in the HTML form but to be overwritten by original value. 在很短的时间内(不到一秒钟),您实际上会在HTML表单中看到用户值,但是会被原始值覆盖。

Wanted results: In HTML info-box the number user added should show up. 所需的结果:在HTML信息框中,应显示用户添加的数字。


Scenario-2: Pressing submit-2 calls function "outsideValidation()", that set's "userInputHours" to 5: 方案2:按Submit-2调用函数“ outsideValidation()”,该函数将“ userInputHours”设置为5:


Works: the global variable "userInputHours" is being updated from within the function. Works:全局变量“ userInputHours”正在从函数内更新。 The console.log(userInputHours) confirmes that global variable has been updated. console.log(userInputHours)确认全局变量已更新。 The HTML info-box is being updated with the update global variabl value. HTML信息框将使用更新的全局变量值进行更新。

Does not work: Since this test is not connected to the user form field "hours", it does not work to add a number in the field "hours" and get it updated onf the HTML info-box. 不起作用:由于此测试未连接到用户表单字段“ hours”,因此无法在“ hours”字段中添加数字并通过HTML信息框对其进行更新。

Expected results: Works as expected. 预期结果:按预期工作。 Does not solve the complete needed solution. 无法解决所需的完整解决方案。

 // Global variables. var userInputHours = 0; // Validation. function validation() { userInputHours = document.getElementById("userInputHours").value; // Works /* Validates blank fields */ if (userInputHours == "") { console.log("A field is left blank"); // Works alert("Please fill in all fields!") // Works return false; } else { console.log("You added: " + userInputHours); // Works // You see added value for a very short moment. The it returns to the original glbal value. document.getElementById("info-box-text").innerHTML = userInputHours; // Does not work. return true; } } /* Not part of validation. Connected to button "submit-2" */ function outsideValidation() { userInputHours = 5; document.getElementById("info-box-text").innerHTML = userInputHours; // Works but is disconnected from HTML form user input. } /* Not part of function. Only run when saving the script. */ document.getElementById("info-box-text").innerHTML = userInputHours; // Works by save the script. 
 .box-1 { background-color: white; width: 100px; height: 100px; } #userInputHours { width: 100px; } #submit-1, #submit-2 { height: 60px; } 
 <form onsubmit = "return validation();"> Hours:<br /> <input id="userInputHours" type="text" name="hours" value=""><br /><br /> <input id="submit-1" type="submit" value="Submit-1 (inside form)"><br /><br /> <!-- Submit button --> </form> <br /> <input id="submit-2" type="submit" value="Submit-2 (outside form)" onclick="outsideValidation()"> <br /><br /> <div id="info-box-title" class="info-box-title">Info-box - (you have set hours to):<br /></div> <div id="info-box-text" class="info-box-text"></div> 

The return value of validation is used by the browser to determine if the default behavior of the form submit should be done. validation的返回值由浏览器用来确定是否应执行表单提交的默认行为。

If the return value is true then the default behavior will take place, and the browser will submit the form to the URL which is defined in the action attribute (if no value is present for the action attribute then it defaults to the current url). 如果返回值是true ,那么默认的行为会发生,浏览器会提交表单到这是在定义的URL action属性(如果没有价值是本作的action属性,那么它默认为当前的URL)。

With a form submit you will leave the current page and load a new one or reload the current one depending on the value of action . 提交表单后,您将离开当前页面并加载新页面或根据action的值重新加载当前页面。 And because of that all value stored with js are lost. 因此,与js一起存储的所有值都将丢失。

If you do not want to send the data to the server you have to write return false in both cases to prevent the form submit. 如果您不想将数据发送到服务器,则在两种情况下都必须编写return false来防止表单提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM