当我引入从函数调用传递的变量时,For 循环无法正常工作
For loop not working correctly when I introduce a variable passed from a function call
由于某种原因,我的验证函数工作得很好,直到我尝试传入从函数收到的变量。目前以下内容工作正常,但是当我取消下面的注释txtValue
时,它不起作用。
function validate(formName) {
// create an object array to hold all html nodes
var formNodes = formName.getElementsByTagName("*");
var formNodesLength = formNodes.length;
// placeholder for error codes
var error = "";
// error style
var borderStyle = "3px solid #bbd531";
var txtValue = "";
// loop and find all nodes with the attribute "validate"
for (x=0; x<formNodesLength; x++) {
if(formNodes[x].getAttribute("data-validation")){
nodeValue = formNodes[x].value;
nameValue = formNodes[x].getAttribute("name");
validateValue = formNodes[x].getAttribute("data-validation");
if (validateValue=="text" && (nodeValue==null || nodeValue=="")) { //if text match failed
alert(nameValue);
/*txtValue = findLabelValue();*/
error += /*txtValue + */" is required<br />";
formNodes[x].style.border=borderStyle;
}
else if (validateValue=="email" && !nodeValue.match(/'b[A-Z0-9._%+-]+@[A-Z0-9.-]+'.[A-Z]{2,4}'b/i)) { //if email match failed
error += "email must be a valid address<br />";
formNodes[x].style.border=borderStyle;
}
else if (validateValue=="phone" && !nodeValue.match(/'b[0-9]{3}-[0-9]{3}-[0-9]{4}'b/)) { //if phone match failed
error += "phone must be a valid<br />";
formNodes[x].style.border=borderStyle;
}
else if (validateValue=="website" && !nodeValue.match(/'b[a-zA-Z0-9'-'.]+'.[A-Z]{2,4}'b/i)) { //if website match failed
error += "website must be a valid address<br />";
formNodes[x].style.border=borderStyle;
}
}
}
if(error) {
var formElement = "<div id='error-box'><strong>There was a problem with your submission</strong><br /></div>" + formName.innerHTML;
formName.innerHTML = formElement;
document.getElementById("error-box").innerHTML = error;
return false;
}
else {
return true;
}
function findLabelValue() {
var labelTagObj = formName.getElementsByTagName("label");
var returnValue = "";
for (x=0; x<labelTagObj.length; x++) {
if (labelTagObj[x].getAttribute("for") == nameValue) {
returnValue = labelTagObj[x].innerText;
return returnValue;
}
}
} // End findLabelValue()
} // End validate(formName)
这是因为您在两个for
循环中使用全局变量x
作为计数器。
调用 findLabelValue()
函数时,它会将全局x
设置为 0
,并运行循环,这会干扰 validate()
函数中的主循环。
在这两个函数中使用var x
以确保它不是全局的。
这是您正在做的事情,缩短以使其更清晰。
function validate(formName) {
// ...
// loop using the `x` variable as a counter
for (x=0; x<formNodesLength; x++) {
if(formNodes[x].getAttribute("data-validation")){
// ...
if (validateValue=="text" && (nodeValue==null || nodeValue=="")) {
// Invoke the other function
txtValue = findLabelValue();
}
// ...
}
}
// ...
function findLabelValue() {
// ...
// OOPS. you've altered the same `x` being used by the other loop
for (x=0; x<labelTagObj.length; x++) {
// ...
}
}
}
在这两个函数中,x=0
都应该var x=0
。
相关文章:
- 为什么JavaScript在for循环为3时向所有4发出警报
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 我的javascript for循环不起作用
- For循环冻结Javascript
- 如何在for循环中添加事件侦听器
- 双“for”循环(循环)
- javascript for循环不起作用
- for循环中的javascript if语句找不到==
- Javascript在for循环中等待处理请求
- 我没有'I don’我不理解for在循环中的用途
- 为什么在 Javascript 中使用空的 for 循环 - for(;;).
- 错误后如何停止循环“FOR”
- 如何使用循环 for 与 java 脚本一起检查输入中的值
- 如何正确使用ngRepeat,同时使用循环for
- 是否可以使用循环(for/while)来制作动画?
- 在循环for循环时如何进入和退出
- Javascript原型循环for in数组
- 在对象数组中循环for
- Javascript循环:for循环工作,但不能映射
- 如何创建一个循环"for"在json