Preventing multiple clicks on button

后端 未结 14 1901
小鲜肉
小鲜肉 2020-12-02 09:43

I have following jQuery code to prevent double clicking a button. It works fine. I am using Page_ClientValidate() to ensure that the double click is prevented o

相关标签:
14条回答
  • 2020-12-02 10:36

    you can use jQuery's [one][1] :

    .one( events [, data ], handler ) Returns: jQuery

    Description: Attach a handler to an event for the elements. The handler is executed at most once per element per event type.

    see examples:

    using jQuery: https://codepen.io/loicjaouen/pen/RwweLVx

    // add an even listener that will run only once
    $("#click_here_button").one("click", once_callback);
    
    0 讨论(0)
  • 2020-12-02 10:36

    This should work for you:

    $(document).ready(function () {
        $('.applicationButton').click(function (e) {
            var btn = $(this),
                isPageValid = Page_ClientValidate(); // cache state of page validation
            if (!isPageValid) {
                // page isn't valid, block form submission
                e.preventDefault();
            }
            // disable the button only if the page is valid.
            // when the postback returns, the button will be re-enabled by default
            btn.prop('disabled', isPageValid);
            return isPageValid;
        });
    });
    

    Please note that you should also take steps server-side to prevent double-posts as not every visitor to your site will be polite enough to visit it with a browser (let alone a JavaScript-enabled browser).

    0 讨论(0)
  • 2020-12-02 10:40

    Just copy paste this code in your script and edit #button1 with your button id and it will resolve your issue.

     <script type="text/javascript">
                    $(document).ready(function(){  
                         $("#button1").submit(function() {
                                $(this).submit(function() {
                                    return false;
                                });
                                return true;
                            }); 
            });
         </script
    
    0 讨论(0)
  • 2020-12-02 10:41

    After hours of searching i fixed it in this way:

        old_timestamp == null;
    
        $('#productivity_table').on('click', function(event) {
    
        // code executed at first load
        // not working if you press too many clicks, it waits 1 second
        if(old_timestamp == null || old_timestamp + 1000 < event.timeStamp)
        {
             // write the code / slide / fade / whatever
             old_timestamp = event.timeStamp;
        }
        });
    
    0 讨论(0)
  • 2020-12-02 10:42

    Disable pointer events in the first line of your callback, and then resume them on the last line.

    element.on('click', function() {
      element.css('pointer-events', 'none'); 
      //do all of your stuff
      element.css('pointer-events', 'auto');   
    };
    
    0 讨论(0)
  • 2020-12-02 10:43

    If you are doing a full round-trip post-back, you can just make the button disappear. If there are validation errors, the button will be visible again upon reload of the page.

    First set add a style to your button:

    <h:commandButton id="SaveBtn" value="Save"
        styleClass="hideOnClick"
        actionListener="#{someBean.saveAction()}"/>
    

    Then make it hide when clicked.

    $(document).ready(function() {
        $(".hideOnClick").click(function(e) {
            $(e.toElement).hide();
        });
    });
    
    0 讨论(0)
提交回复
热议问题