Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我引入从函数调用传递的变量时,For循环无法正常工作_Javascript - Fatal编程技术网

Javascript 当我引入从函数调用传递的变量时,For循环无法正常工作

Javascript 当我引入从函数调用传递的变量时,For循环无法正常工作,javascript,Javascript,出于某种原因,在我尝试传入从函数接收的变量之前,我的验证函数工作得很好。目前,下面的操作正常,但是当我取消下面的txtValue注释时,它就不起作用了 function validate(formName) { // create an object array to hold all html nodes var formNodes = formName.getElementsByTagName("*"); var formNodesLength = formNodes.length; //

出于某种原因,在我尝试传入从函数接收的变量之前,我的验证函数工作得很好。目前,下面的操作正常,但是当我取消下面的
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)
函数验证(formName){
//创建一个对象数组以容纳所有html节点
var formNodes=formName.getElementsByTagName(“*”);
var formNodesLength=formNodes.length;
//错误代码的占位符
var误差=”;
//错误样式
var borderStyle=“3px固体#bbd531”;
var txtValue=“”;
//循环并查找属性为“validate”的所有节点

对于(x=0;x这是因为在两个
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++) {
            // ...
        }
    } 
}
函数验证(formName){
// ...
//使用'x'变量作为计数器进行循环

(x=0;XOMG,谢谢!我已经看了这么久了,结果总是很明显的。”USER 174315:考虑使用一个Link来检查像这样的常见错误。JSHITTN非常好,已经清理了!再次感谢: