[英]Variable values not being updated across functions
The variable Message
and boolean value count
from function validator
won't get updated when it runs inputFocus
function. 当运行inputFocus
函数时,来自函数validator
的变量Message
和布尔值count
不会被更新。 So, validator
always returns true. 因此, validator
始终返回true。
(function(){
var form = document.forms[0];
form.addEventListener('submit', function(evt){
if(!validator(form)){
evt.preventDefault();
}
}, false);
})();
main.js main.js
function validator(form){
var message = "The input below is invalid";
var count = true;
if(!phoneValidation(form.phone.value)){
inputFocus(form['phone'], "focus", message, form.phone.name, count);
}
if(!count){
alert(message);
return count;
} else {
return true;
}
}
function inputFocus(element, newClassName, message, id, count){
element.className += " " + newClassName;
message += ", " + id;
if (count){
count = false;
}
}
function inputFocus(element, newClassName, message, id, count){
element.className += " " + newClassName;
message += ", " + id;
if (count){
count = false;
}
}
So when you pass the parameters (including message
and count
) to inputFocus
, given the fact that they are scalars (a boolean and a string), then whatever inputFocus
does with the variables local to it "stays" within inputFocus
. 因此,当您将参数(包括message
和count
)传递给inputFocus
,鉴于它们是标量 (一个布尔值和一个字符串),那么无论inputFocus
局部变量执行的操作,都会“停留”在inputFocus
。 This is because it's passed the values of the scalars, not the variables per se. 这是因为它传递了标量的值 ,而不是变量本身。
inputFocus
doesn't change whatever variables whose values happened to be passed to it. inputFocus
不会更改碰巧将其值传递给它的任何变量。
One potential way to solve this is to have inputFocus
return the correct values of count
and message
, then validator
can do as it wishes with those values. 解决此问题的一种可能方法是让inputFocus
返回count
和message
的正确值,然后validator
可以根据需要使用这些值进行操作。
Hope this gets you on the right track. 希望这能使您走上正确的道路。
Move the variables Message and count outside all the functions to make it like global. 将变量Message移到所有函数之外并计数,以使其类似于global。 This will let any javascript function in the page to update them 这将允许页面中的所有javascript函数更新它们
var message;
var count;
function validator(form){
message = "The input below is invalid";
count = true;
if(!phoneValidation(form.phone.value)){
inputFocus(form['phone'], "focus", message, form.phone.name, count);
}
if(!count){
alert(message);
return count;
} else{
return true;
}
}
function inputFocus(element, newClassName, message, id, count){
element.className += " " + newClassName;
message += ", " + id;
if (count){
count = false;
}
}
At the same time its not a good practice to use global variables if you can merge the functions that uses these variables into one. 同时,如果您可以将使用这些变量的函数合并为一个,则不建议使用全局变量。
That's because when you set the value of message
and count
within the inputFocus
function, the value of those variables are only available within the scope of that function. 这是因为当您在inputFocus
函数中设置message
的值并count
,这些变量的值仅在该函数的范围内可用。
Here's an evening's worth of reading. 这是一个值得一读的夜晚。 Absolutely worth it if you want to write javascript. 如果您想编写javascript,绝对值得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.