问题
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?
回答1:
Remove the returns from the onsubmit and add them to the functions:
onsubmit="return (formCheck(this) && validate_dropdown())"
回答2:
onsubmit="return (formCheck(this) && validate_dropdown())"
回答3:
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 like return 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.
回答4:
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.
<form onsubmit="return (function1(this) && function2(this))">
Method B.
<script type="text/javascript">
function formValidation(myForm)
{
var result = function1(myForm);
result = result && function2(myForm);
return result;
}
</script>
<form onsubmit="return formValidation(this)">
来源:https://stackoverflow.com/questions/3107061/how-to-add-two-on-submit-values-to-a-form