form submit拦截 表单提交拦截

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-29 09:59   708   0

[code]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>previous form submit</title>
<script type="text/javascript">
posted = false;
function submitForm(form)
{
form.submit();
}

/**
* This is a form submit interceptor.
* In order to avoid duplicate submitting error, it will check posted flag. if posted flag is still false, will set it to true.
* Usage:
* <body οnlοad="formSubmitInterceptor();">
* @author yangjincheng
*/
function formSubmitInterceptor()
{
var forms = document.getElementsByTagName("form");
if(!forms.length)
{
//if no form tag exists.
return;
}
for(var i = 0; i < forms.length; i++)
{
form = forms[i];
form.preFormSubmit = form.submit;
form.submit = function(){
if(typeof posted == "undefined")
{
alert("You must define variable: posted !!!");
return;
}
if(posted)
{
alert("form is submitted!");
return;
}
//1: When form submit, if posted is still false, set it to true.
if(!posted)
{
posted = true;
}
//2: Then submit form.
alert("Previous submit, you can add your code here...");
form.preFormSubmit();
}
}
}

</script>
</head>
<body οnlοad="formSubmitInterceptor();">
<form action="http://wwww.google.com" method="get" enctype="application/x-www-form-urlencoded">
<input name="name1" />
<input name="name2"/>
<input type="button" οnclick="submitForm(this.form);" value="提交">
</form>
</body>

</html>

[code]

转载于:https://my.oschina.net/fdhay/blog/17508

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP