onsubmit多个JavaScript函数 [英] onsubmit multiple javascript functions

查看:127
本文介绍了onsubmit多个JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何提交多个函数?我想要做的是:onsubmit检查所有这些函数是否返回true,如果所有这些函数都返回true,那么我执行action =checked.html



<$ p $ ()| validatePhone()|}返回validateName()方法=post
onsubmit =return validateName()|| validatePhone()| | validateAddress()||
validateEmail()>

然而,实际发生的情况是,计算机从左到右检查代码,下一个函数将替换前一个函数,依此类推,直到结束。因此,即使所有3个第一个函数都返回false,只要最后一个是真的,计算机将执行action =checked.html,这不是我想要做的事......任何人都可以请帮助我这是4个小时,我试图解决它:S谢谢!



另外,当我试图做一些像< form onsubmit =return verify1()&& verify2()> ,它不起作用,它只是检查verify1(),而不是下面的那些... :


$ b

CLARIFCATION:在提交时,我需要调用四个验证函数,除非所有四个函数都返回true,否则应该阻止提交,但所有四个函数都应该始终即使它们中的一些返回false,也是如此。 使用& 而不是&&



&& 使用短路评估,因此如果左侧操作数是虚假的,则不评估右侧操作数。 p>

& 总是计算两个操作数。 > onsubmit =return !!(validateName()& validatePhone()
& validateAddress()& validateEmail());

编辑:对不起,我忘记了& 不会返回实际的布尔值,将表达式放在圆括号中,并使用双将其转换为(now)如果在中使用结果,如果 test,则不必担心,但事件处理函数的返回值应该是实际的布尔值。)



PS:这是一个相当笨重的演示,显示所有四个函数被调用,但会产生您需要的全部真或假结果: http://jsfiddle.net/nnnnnn/svM4h/1/


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"

<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!

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... :(

CLARIFCATION: On submit, I want four validation functions to be called. Submit should be prevented unless all four functions return true, but all four should always be called even if some of them return 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.)

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/

这篇关于onsubmit多个JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆