onsubmit multiple javascript functions

后端 未结 7 615
你的背包
你的背包 2020-11-30 08:31

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

7条回答
  •  醉话见心
    2020-11-30 08:51

    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.)

    P.S.: 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/

提交回复
热议问题