表单中是否允许使用多个 onsubmit javascript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9025667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are multiple onsubmit javascript functions allowed in a forms?
提问by Joshua Davis
Are multiple onsubmit functions allowed in forms? Example:
表单中是否允许使用多个 onsubmit 函数?例子:
<form name="form" method="POST" action="contactus.asp" onsubmit="return verify()"
<!--*****CAN I ADD ANOTHER onsubmit FUNCTION HERE?***** -->>
回答by Adam Zalcman
Well, you can add any legal JavaScript statement including this:
好吧,您可以添加任何合法的 JavaScript 语句,包括:
<form onsubmit="return verify1() && verify2()">
or this
或这个
<form onsubmit="return verify1() || verify2()">
or more complex expressions which can include multiple function calls. Note that standard evaluation rules apply including short-circuit evaluation of logical expressions.
或更复杂的表达式,其中可以包含多个函数调用。请注意,标准评估规则适用,包括逻辑表达式的短路评估。
回答by Tango Bravo
I believe that wouldn't work. You should just make a wrapper function, and include the functions which you desire to execute, inside of that.
我相信那是行不通的。您应该只制作一个包装函数,并在其中包含您希望执行的函数。
<script type="text/javascript">
var run1 = function() { alert("first"); }
var run2 = function() { alert("second"); }
function sub() {
run1();
run2();
}
</script>
<form name="form" method="POST" action="contactus.asp" onsubmit="javascript:sub();">