开发者

use several JS function on form check

i have a select box to allow the user select what kind of form he need to fill in.

the 2 options are for a single person or someone from a group therefore some of the fieldsets that are in use in the single are switched with some from the group ones.

so for the single user i have this functions: singleinformation, winninginformation, reporterinformation, parentalinformation. and for the group i have groupinformation, winninginformation, reporterinformation.

in the onsubmit i have another check() function that will check the check box and by the selection will call the right functions

function Check() {
    if (document.getElementById("type").value=="s") {
开发者_如何学Python        return CheckSingleInformation();
        return CheckWinningInformation();
    }
    else if (document.getElementById("type").value=="g") {
        return CheckGroupInformation();
        return CheckWinningInformation();
    }

}

for some reason, after finishing the first function, if it returned without false it will send the form and not stop at the first error of the second function

what can i do to fix it?


onsubmit must have as value "return Check()". If you simply call Check(), returning values won't be used and it will continue with the submission.


never mind... found the answer :-)

first i have changed the main function to look like this:

function Check() { 
    if (document.getElementById("type").value=="s") { 
        CheckSingleInformation(); 
        CheckWinningInformation(); 
    } 
    else if (document.getElementById("type").value=="g") { 
        CheckGroupInformation(); 
        CheckWinningInformation(); 
    } 

} 

but then i found out it wont stop after the first function so i changed it to be like this

function Check() {
    if (document.getElementById("type").value=="s") {
        if (CheckSingleInformation()==false) {return false}
        if (CheckWinningInformation()==false) {return false}
    }
    else if (document.getElementById("type").value=="g") {
        if (CheckGroupInformation()==false) {return false}
        if (CheckWinningInformation()==false) {return false}
    }
}

is that the right way?


2 notes:

  • Returning "true" from the onsubmit() event will stop it from propagating; the form won't be submitted (usually is the behaviour you want when you're doing an AJAX posting)
  • checkWinningInformation(); will never be called in both blocks, since the statement prior to it is a "return" which actually jumps out of the method execution... that can't be what you want, can it?


Check nested conditions,

function Check() {
    if (document.getElementById("type").value=="s") {
        if(!CheckSingleInformation()){
           return CheckWinningInformation();
        }
        return true;
    }
    else if (document.getElementById("type").value=="g") {
        if(!CheckGroupInformation()){
            return CheckWinningInformation();
        }
        return true;
    }

}


Return is used in a function for two reasons:

  • You want the script to stop executing if something happens.
  • You want the function to return a value to the calling function.

You used return like so:

return CheckSingleInformation(); <-- exit here
return CheckWinningInformation();

Your script will "exit" (or submit the form) right after the first return!

What you could do to resolve this: Call CheckWinningInformation() at the end of CheckSingleInformation() as well as at the end of CheckGroupInformation().


Using logical AND operator (&&) would be fine. so the following code :

function Check() {
    if (document.getElementById("type").value=="s") {
        return CheckSingleInformation();
        return CheckWinningInformation();
    }
    else if (document.getElementById("type").value=="g") {
        return CheckGroupInformation();
        return CheckWinningInformation();
    }

}

could be :

function Check() {
    if (document.getElementById("type").value=="s") {         
        return (CheckWinningInformation() && CheckSingleInformation());
    }
    else if (document.getElementById("type").value=="g") {        
        return (CheckWinningInformation() && CheckGroupInformation());
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜