如何添加两个“on Submit=”值到形式?
我正在尝试使用两个单独的 JavaScript 函数来验证我的表单:
<form onsubmit="return formCheck(this); return validate_dropdown();"
action="somepage.php"
method="post"
name="something">
当我在提交值上仅添加一个函数时,每个函数都可以单独正常工作,而当我添加两个函数时,仅第一个函数的验证有效,第二个函数的验证无效。这里有什么问题吗?
I am trying to validate my form using two separate JavaScript functions:
<form onsubmit="return formCheck(this); return validate_dropdown();"
action="somepage.php"
method="post"
name="something">
When I add just one on submit value, each function works fine individually, when I add two only the first function's validation works, not the second. What is wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 onsubmit 中删除返回并将其添加到函数中:
onsubmit="return (formCheck(this) && validate_dropdown())"
Remove the returns from the onsubmit and add them to the functions:
onsubmit="return (formCheck(this) && validate_dropdown())"
我没有足够的声誉来投票赞成或反对,但我可以向您保证,当有两个回报时,例如:
return formCheck(this) && return validate_dropdown();
它需要写成return formCheck(this) && validate_dropdown();
...省略第二个“返回”。
所以这个帖子上得票最多的回复实际上是错误的,Anax 的 0 票回复(在撰写本文时)解决了我的问题。其他人在 IE 中产生脚本错误。
I don't have enough reputation to vote up or down, but I can assure you that when having two returns, like:
return formCheck(this) && return validate_dropdown();
it needs to be written likereturn formCheck(this) && validate_dropdown();
...leaving out the second 'return'.
So the replies with the most votes on this thread are actually wrong, and Anax's reply with 0 votes (at the time of writing) solved my problem. The others produced script errors in IE.
您的代码将不起作用,因为一旦执行第一个函数(
formCheck
),它将返回一个值,该值将允许或拒绝表单提交。如果您想同时使用两个或多个函数,您可以组合它们的结果或编写一个新函数,该函数依次运行两个验证函数并返回结果。
方法 A.
方法 B.
Your code will not work, because, as soon as the first function (
formCheck
) is executed, it will return a value, which will either allow or deny form submission.If you have two or more functions you want to use at the same time, you can either combine their results or write a new function, which in turn will run both your validation functions and return the results.
Method A.
Method B.