开发者

Browser on submit with jquery

On submit of a form I show a blocking message "Please wait" with following code:

$(document).ready(function() { 
  $('form').submit(function() {
    $.blockUI({ message: "<h1 id='waitingmessage'>Plz wait ...</h1>" });
  });
}); 

That works ok for me.

If the user presses F5 now, the browser ask for resubmit. The user is allowed to. But the script above does not trigger anymore.

How can I trigger this resubmit with jquery, thus the waitingmessage is still shown?

Thx

Chris

EDIT: The submit of the form goes to the same adress as the form. I only distinct on the controller side between GET and POST. If it was a POST, I process the received data, otherwise I show only the blank form. The user is allowed to send data twice or as often as wanted. So after the first POST the user can press F5 to resubmit the data, and he is totally allowed to. It's just that the waitingm开发者_运维知识库essage does not appear any more. So I need maybe a more general solution "onpost". Does something like that exist?

SOLUTION:

$(document).ready(function() { 
   $('form').submit(function() {
      $.blockUI({ message: "<h1 id='waitingmessage'>Bitte warten ...</h1>" });
   });

   $(window).keydown(function(event){
      if (event.keyCode == '116' && '${request.method}' == 'POST') {
         $.blockUI({ message: "<h1 id='waitingmessage'>Bitte warten ...</h1>" });
      }
   });
});

And request.method is set server side and is either 'GET' or 'POST' thus I know where the user came from.


It seems that I don't have perfect solution too. I will do only for only first F5 event

<script type="text/javascript">
$(function ()
{
    $(window).keydown(function (e)
    {

        if(e.keyCode == 116){
            submitform();
            return false;
        }
        alert(e.keyCode);
    });
})

function submitform()
{
    alert("hello");
}
</script>

<form id="myform" action='test.php'>
    <input id="submit" type="submit" onclick="submitform(); return false;"/>
</form>


You don't, because when the user press F5, the browser make a refresh of the last "Query" and the last Query is the POST of the form, not the display of the form.

In your history, you have something like this :

  • GET : showForm.php (Form with JS code)
  • POST : sublitForm.php (Form processing)

When you press F5, the browser perform a refresh on the POST.

EDIT: You can perhaps make a HACK, using an Ajax call or using $('#myForm').submit();. And after the result, use a location.href to redirect the user.

If the user perform an refresh, you'll don't have the post in you sequence.


Using jquery to prevent resubmitting form chech this link

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜