form submit攔截 表單提交攔截

[code]javascript

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

<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 onload="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 onload="formSubmitInterceptor();">
<form action="http://wwww.google.com" method="get" enctype="application/x-www-form-urlencoded">
<input name="name1" />
<input name="name2"/>
<input type="button" onclick="submitForm(this.form);" value="提交">
</form>
</body>

</html>java

[code]app

相關文章
相關標籤/搜索