简体   繁体   English

提交多个javascript函数

[英]onsubmit multiple javascript functions

Does anyone know how to onsubmit multiple functions? 有谁知道如何提交多个功能? What I intented to do was: onsubmit checks if all these functions return true, if all of them return true, then I do the action="checked.html" 我打算做的是:onsubmit检查所有这些函数是否返回true,如果所有函数都返回true,那么我执行action =“checked.html”

<form id="myForm" name="myForm" action="checked.html" method="post"
onsubmit="return validateName() || validatePhone() || validateAddress() ||
validateEmail()">

However, what actually happened was, the computer checks the code from left to right, and the result of the next function will replace the previous one, and so on, until the end. 然而,实际发生的是,计算机从左到右检查代码,下一个函数的结果将替换前一个,依此类推,直到结束。 So, even if all the 3 first functions return false, as long as the last one is true, the computer will execute action="checked.html", which is not what I intented to do... can anyone please help me on this, it's been like 4 hours I'm trying to fix it :S Thanks! 所以,即使所有3个第一个函数都返回false,只要最后一个函数为true,计算机就会执行action =“checked.html”,这不是我打算做的......有人可以请帮助我这个,就像4小时我试图解决它:谢谢!

Also, when I tried to do something like <form onsubmit="return verify1() && verify2()"> , it doesn't work, all it does is to check verify1(), and not the following ones... :( 此外,当我尝试执行类似<form onsubmit="return verify1() && verify2()"> ,它不起作用,它所做的只是检查verify1(),而不是以下那些......: (

CLARIFCATION: On submit, I want four validation functions to be called. CLARIFCATION:在提交时,我想要调用四个验证函数。 Submit should be prevented unless all four functions return true, but all four should always be called even if some of them return false. 除非所有四个函数都返回true,否则应该阻止提交,但是即使其中一些函数返回false,也应始终调用所有四个函数。

Use & instead of && . 使用&而不是&&

&& uses short-circuit evaluation and so doesn't evaluate the right-hand operand if the left-hand operand is falsy. &&使用短路评估,因此如果左侧操作数是假的,则不评估右侧操作数。

& always evaluates both operands. &始终评估两个操作数。

onsubmit="return !!(validateName() & validatePhone()
                      & validateAddress() & validateEmail());"

EDIT: Sorry, I forgot that & won't return an actual boolean. 编辑:对不起,我忘了&不会回到真正的布尔值。 Wrap the expression in parentheses and use a double ! 将表达式包装在括号中并使用双精度! to convert it as (now) shown. 将其转换为(现在)显示。 (You wouldn't need to worry if using the result in an if test, but the return value from the event handler should be an actual boolean.) (如果在if测试中使用结果, if无需担心,但事件处理程序的返回值应该是实际的布尔值。)

PS: Here's a rather clunky demo to show that all four functions get called but produce the overall true or false result that you need: http://jsfiddle.net/nnnnnn/svM4h/1/ PS:这是一个相当笨重的演示,表明所有四个函数都被调用但产生了你需要的整体真假结果: http//jsfiddle.net/nnnnnn/svM4h/1/

Change your code to use AND instead of OR 将代码更改为使用AND而不是OR

onsubmit="return validateName() && validatePhone() && validateAddress() && validateEmail()" onsubmit =“return validateName()&& validatePhone()&& validateAddress()&& validateEmail()”

The reason being that if you use OR once that first function returns true javascript won't bother to check the other functions. 原因是,如果你使用OR,第一个函数返回true javascript将不会费心去检查其他函数。 Also you don't want to use OR because if the first check was false and second is true the form will still submit since at least one of your checks is TRUE. 此外,您不想使用OR,因为如果第一次检查为false且第二次检查为true,则表单仍将提交,因为至少有一项检查为TRUE。

See fiddle to show this works, both checks execute and show an alert message but because check2 returns false the form doesn't submit - if you want play around with the return results of checks1 and check2 functions and the use of && and || 请参阅小提示以显示此作品,检查执行并显示警告消息,但因为check2返回false,表单不提交 - 如果您想要使用checks1和check2函数的返回结果以及使用&&和||

After understanding the problem better, you need to use a single &, but because true & true returns 1 and not true you need to write it like this 在更好地理解问题之后,你需要使用单个&,但因为true & true返回1并且不是true你需要像这样写它

onsubmit="return ((validateName() & validatePhone() & validateAddress() & validateEmail()) == 1)" onsubmit =“return((validateName()&validatePhone()&validateAddress()&validateEmail())== 1)”

if all you need is make sure all functions return true before proceeding, then use && not || 如果你需要的是确保所有函数在继续之前返回true,那么使用&& not || and to insure that the expression does not get evaluated pre-maturally by the return function put some extra brackets 并确保表达式不会在返回函数的前期得到一些额外的括号

<form id="myForm" name="myForm" action="checked.html" method="post"
onsubmit="return (validateName() && validatePhone() && validateAddress() &&
validateEmail() )">

Create a wrapper function, which call each function and checks their return value. 创建一个包装函数,它调用每个函数并检查它们的返回值。 At any time if the return value is false you can either stop the form submit by returning false at that time or evaluate the rest of the things and finally return false. 在任何时候如果返回值为false,您可以通过在此时返回false来停止表单提交,或者评估其余的事情并最终返回false。

So JS a code that will do what you want is : 所以JS代码可以做你想要的:

function validateForm() {
    var isFormValid = true;
    isFormValid &= validateName();
    isFormValid &= validatePhone();
    isFormValid &= validateAddress();
    isFormValid &= validateEmail();
    return isFormValid? true:false;
}

Now in the form simply use : 现在在表单中使用:

<form id="myForm" name="myForm" action="checked.html" method="post" onsubmit="return validateForm();">

Here is a working JSFiddle Example. 这是一个有效的JSFiddle示例。

EDIT : Made a boo boo below, corrected. 编辑:下面做了一个嘘声,纠正。

Using OR was incorrect, but the reason AND is not working how you are expecting is because of Short-circuit_evaluation . 使用OR是不正确的,但是因为Short-circuit_evaluation而导致AND无法正常工作的原因。

  • OR : Evaluation stops on first operand which is true and true is returned OR:评估在第一个操作数上停止,返回true和true
  • AND : Evaluation stops on first operand which is false and false is returned AND:评估在第一个操作数上停止,该操作数为false,返回false
  • onsubmit="return validateName() && validatePhone() && validateAddress() && validateEmail()" onsubmit =“return validateName()&& validatePhone()&& validateAddress()&& validateEmail()”

    use this code 使用此代码

    To validate my form I simply created a function called validateForm(){} then inside this function I put all the code which is needed for full validation. 为了验证我的表单,我只是创建了一个名为validateForm(){}的函数,然后在这个函数中我放了完整验证所需的所有代码。 Then used onsubmit="validateForm()". 然后使用onsubmit =“validateForm()”。 It checks through it in order and only submits if all conditions are true. 它按顺序检查它,只有在所有条件都为真时才提交。

    Following method check first validateName() function and it will process to check next function if only first function is true, also to fire third function need to return true of first & second function. 下面的方法首先检查validateName()函数,如果只有第一个函数为真,它将处理检查下一个函数,同时激活第三个函数需要返回第一个和第二个函数的真值。 So just imagine if all function are invalid & even you have different error message, you will see only error message of first function. 所以想象一下,如果所有函数都无效甚至你有不同的错误信息,你只会看到第一个函数的错误信息。 Once validateName() function pass, you may see the error message of validatePhone() ect. 一旦validateName()函数通过,您可能会看到validatePhone()等错误消息。

    <form id="myForm" name="myForm" action="checked.html" method="post"
    onsubmit="return (validateName() && validatePhone() && validateAddress() &&
    validateEmail() )">
    

    You can't use above if you going to show error messages according to above all validations on first submit & you can use following method if you want to fire all function (or display all validation errors) on submit. 如果要在首次提交时根据上述所有验证显示错误消息,则无法使用上述内容;如果要在提交时触发所有功能(或显示所有验证错误),则可以使用以下方法。

    <form id="myForm" name="myForm" action="checked.html" method="post"
    onsubmit=" if( ( validateName() && validatePhone() && validateAddress() &&
    validateEmail() ) == false) { return false; } ">
    

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

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