zoukankan      html  css  js  c++  java
  • 【KMS】如何预防用户多次点击提交按钮

    如何避免页面多次提交?

    How to prevent user clicking on submit button for mutiple times?

    In general, we can use javascript below to implement it. We need to run js function
    postRender(this) in web page's onload event.
    For example:

    var submitted = false;
    var multiple = false;
    //apply styles/behaviours after page load
    function postRender(obj) {
        //no support for NS4
        if (!document.getElementsByTagName)
            return;
        //no support for non-HTTP
        if (window.location.protocol != "http:" && window.location.protocol != "https:")
            return;
        //go through form elements, adding functionality
        var inputs = document.getElementsByTagName('input');
        for (var i=0;i<inputs.length;i++) {
            //add handler to prevent multiple submits
            if (inputs[i].type=='button' || inputs[i].type=='submit' || inputs[i].type=='reset') {
                if (inputs[i].className=='multiple')
                    inputs[i].onclick = function () { multiple = true; return true; } ;
            }
        }
        //add form submit handler
        var forms = document.getElementsByTagName('form');
        for (var i=0;i<forms.length;i++) {
            forms[i].onsubmit = function () {
                      //disable multiple form submits
                     if (submitted) {
                          alert("Your request is still being processed. Pleasewait...");
                          return false;
                      }
                     if (multiple!=true)
                           submitted = true;
                      multiple = false;
            } ;
        }
    }

    Note:

    1. Sometimes you maybe allow some buttons are clicked for mutiple times, such as
    download button. you need to specify class="multiple" for such buttons.
    For example:
    <input type="submit" class="multiple" name="Download Excel" value="Download Excel">

    2. There is another important thing to remember for web page programmer.
    Don't try to submit the form on onclick event of submit button.
    For example:
    <input type="submit" name="Submit" value="Submit" onclick="SubmitForm()">
    Please use type="button" instead of type="submit" to avoid that the form are submittwice.
    <input type="button" name="Submit" value="Submit" onclick="SubmitForm()">

  • 相关阅读:
    PS学习
    这是一个忧伤的故事
    数学规律——约瑟夫环问题(未完结)
    翻车总结——训练场新手村P1421 小玉买文具(C++)
    快速读入——卡常终结者(快读)
    循环练习——亲和数(未完结)
    线性DP——回文词IOI2000(未完结)
    springboot整合(预加载数据commandlinerunner)
    第一章 spring 基础
    第二章 spring 基础
  • 原文地址:https://www.cnblogs.com/orientsun/p/2610420.html
Copyright © 2011-2022 走看看