Sometimes we need to submit a form from one page to a different page. The processing method is generally to dynamically give the action value in the onClick event, as in the following example:
At this time, the verification of the form for different submission targets can only be placed in the onClick event, but this causes the problem that the form will be submitted regardless of whether it passes verification or not. Because this button is of submit type.
My solution to this problem is this:
<script language="JavaScript"><!-- document.returnValue=true;//一个全局变量,给初值。 function validateForm() { var errors;errors='';if (document.PostTopic.title.value=="")errors="标题不能为空";if( document.PostTopic.intro.value.length>10)errors+="\n简介不能多于10个字";if (errors!='') alert(errors);document.returnValue = (errors == '');} file://--></script> <FORM ACTION="" METHOD="post" NAME="PostTopic" onSubmit="return document.returnValue;"><input type=text name=title value=""><input type=text name=intro value=""><INPUT TYPE="submit" NAME=Submit VALUE="新增" class=buttonface onclick="document.PostTopic.action='addone.php';validateForm(); return document.returnValue;"><INPUT TYPE="RESET" NAME=Reset VALUE="重置" class=buttonface ><INPUT TYPE="submit" NAME="Submit" VALUE="修改" class=buttonface onclick="document.PostTopic.action='modify.php';validateForm(); return document.returnValue;"></FORM>