简体   繁体   English

用户单击浏览器后退按钮时提交表单

[英]submit form when user clicks browser back button

I have a situation here. 我有一个情况。 I have a form which contains hidden value. 我有一个包含隐藏价值的表格。 When user clicks the browser back button then the form should be submitted. 当用户单击浏览器后退按钮时,应该提交表单。 I have controlled the back space button but haven't found a solution for browser back button. 我控制了后退空间按钮,但没有找到浏览器后退按钮的解决方案。 Here is my code. 这是我的代码。

Form 形成

 <form id="movevalue" action="/media" method="post">
    <input type="hidden" id="pagedetail" name="pagedetail" value="<?php echo $pagenumber; ?>" />
    <input type="submit" value="" style="display:none" />
 </form> 

At the end of page this is my script. 在页面的末尾这是我的脚本。

script 脚本

    <script>
    $(function(){  
      $(document).bind("keydown keypress", function(e){
      if( e.which == 8 ){ // 8 == backspace
        $('#movevalue').submit();
      }
     });
    });
   window.onbeforeunload = function(evt)    
   {    
    if (typeof evt == 'undefined')     
        $('#movevalue').submit() 
    }

   </script>

window.onbeforeunload this not work for me. window.onbeforeunload这对我不起作用。

Just use this code and this will cover all state of page leave or close... 只需使用此代码,这将涵盖所有页面离开状态或关闭...

<script>
window.onbeforeunload = function() {
    $('#movevalue').submit();
   return true;
};
</script>

try this: 尝试这个:

$(function () {
if (window.history && window.history.pushState) {
    window.history.pushState('', null, '');
    $(window).on('popstate', function (id) {
        $('#movevalue').submit();
    });
}

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 点击浏览器后退按钮时提交表单 - submit a form when hitting the browser back button 当用户单击浏览器 React Js 中的后退按钮时关闭模式 - Close a modal when user clicks back button in browser React Js 当用户单击浏览器的后退按钮时隐藏div - Hide div when user clicks browser's back button 用户单击浏览器的“后退”按钮时清除文本字段/文本框 - Clearing textfields/textboxes when user clicks the browser's Back button 当用户单击提交时,有没有办法将值从按钮传递到表单? - Is there a way to pass the value from a button to a form when the user clicks submit? 用户点击它时隐藏(或禁用)提交按钮 - Hide (or disable) submit button when user clicks on it 当用户双击提交按钮时,需要避免向服务器提交两次表单 - Need to avoid submit form twice to the server when user double clicks on submit button 如何查找用户是单击浏览器后退按钮还是“刷新”按钮 - How to find whether the user clicks browser back button or Refresh button 当用户按下F5键或单击浏览器的后退/关闭按钮时,如何显示自定义的花式框? - How to display customized fancybox when user presses F5 key or clicks browser's back / close button? 当用户单击 MVC5 中的浏览器后退按钮时,在检索数据时遇到问题 - Facing issue in retrieving data when user clicks on browser back button in MVC5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM