How to show a loading gif mean while a submit form is being executed JQuery

前端 未结 5 372
甜味超标
甜味超标 2020-12-16 00:59

I\'m trying to show a simple spinner gif meanwhile a submit is being executed (it takes a bit because I have to establish some communications with providers via web services

相关标签:
5条回答
  • 2020-12-16 01:29

    The real issue with your implementation is how does the client side know when your request has completed. One of the reasons why Ajax is helpful is because we have a notion of states and therefore we can display a loader while the client is waiting for a response and remove the loader when the client receives a response.

    How are you going to determine when the communication with the web service is completed? You say that you don't want to use Ajax, but if you are going to want to query a service to determine whether your communication completed, you are probably seeing something that will require Ajax.

    0 讨论(0)
  • 2020-12-16 01:37

    Have you tried loading the image on document ready, only having it hidden. Then display it in your function? This way, the image is already loaded and spinning.

    $(document).ready(function() {
        $("body").prepend('<div id="overlay" class="ui-widget-overlay" style="z-index: 1001; display: none;"></div>');
        $("body").prepend("<div id='PleaseWait' style='display: none;'><img src='/images/spinner.gif'/></div>");
    });
    
    $('#gds_form').submit(function() {
        var pass = true;
        //some validations
    
        if(pass == false){
            return false;
        }
        $("#overlay, #PleaseWait").show();
    
        return true;
    });
    
    0 讨论(0)
  • 2020-12-16 01:45

    Using the jQuery submit function, you can specify a loading image like this:

    $('#form').submit(function() {
        $('#wait').show();
        $.post('/somepage.htm', function() {
            $('#wait').hide();
        });
        return false;
    });
    
    0 讨论(0)
  • 2020-12-16 01:45

    I would the div on the page to start with, but with style='display:none' then stick a $('#PleaseWait').show() in your code when you want it to appear.

    If you leave the display:none off you can edit see what it looks like without needing to submit the form again and again.

    NB: you may want to wrap the overlay and the gif in a parent div for convenience.

    0 讨论(0)
  • 2020-12-16 01:50

    Use .show() and .hide() to toggle the loading animation while it sends?

    0 讨论(0)
提交回复
热议问题