• 【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()">

  • 相关阅读:
    前端路上的设计道
    缓存实现和处理(微信小程序)
    常见的js中的DOM操作
    在前端页面开发中所遇到的问题总结
    webstrom快捷键设置
    c# html 导出word
    c# html 导出excel
    C#通过gridview导出excel
    关于 应用程序池 'DefaultAppPool' 提供服务的进程意外终止-的一种解决办法
    GridView的RowCommand事件中获取每行控件的值
  • 原文地址:https://www.cnblogs.com/orientsun/p/2610420.html
Copyright © 2020-2023  润新知