您的位置:首页 > 其它

多重提交表单的校验方法

2001-06-22 12:58 246 查看
有时我们需把一个页面的表单提交到不同的页面,处理方法一般采用在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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: