`
angeldhp
  • 浏览: 58319 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

多重提交表单的校验方法

    博客分类:
  • Java
阅读更多

有时我们需把一个页面的表单提交到不同的页面,处理方法一般采用在onClick事件中动态给出action的值,如下例:

<FORM ACTION="" METHOD="post" NAME="PostTopic">

 <INPUT TYPE="submit" NAME=Submit VALUE="新增" class=buttonface onclick="document.PostTopic.action='addone.php';">
    <INPUT TYPE="RESET" NAME=Reset VALUE="重置" class=buttonface >
    <INPUT TYPE="submit" NAME="Submit" VALUE="修改" class=buttonface onclick="document.PostTopic.action='modify.php';">
</FORM>   

此时针对不同提交目标对表单作的校验,只能放在onClick事件中,但这样造成无论是否通过验证都会提交表单的问题。因为这个按钮是submit类型。

对此问题,我的解决方法是这样的:

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics