简体   繁体   English

如果我不按写按钮,我想显示一个警报

[英]If I don't press write button, I want to show an alert

I assign the element id "btn_submit" to write_ok button. 我将元素ID“ btn_submit”分配给write_ok按钮。

I wanted the following: 我想要以下内容:

if I press write_ok button, 如果我按下write_ok按钮,
do write, 确实写
else (if I do not press write_ok button) 否则(如果我不按write_ok按钮)
do alert message before page exit. 在页面退出之前发出警报消息。

I don't know how can i find out which button was pressed. 我不知道如何找到按下了哪个按钮。

I found this, but I cannot get it to work: jQuery: how to get which button was clicked upon form submission? 我找到了这个,但我无法使它正常工作: jQuery:如何获取表单提交时单击的按钮?

<script type="text/javascript">
$(window).bind('beforeunload', function(){
    if ( #btn_submit was not pressed )
        return 'If you exit the page, you may lose your changes';
});
</script>

You can do something like, 你可以做类似的事情,

<script type="text/javascript">
  var btn_submit_pressed = false;

  $(window).bind('beforeunload', function(){
      if ( btn_submit_pressed )
          return 'If you want to exit page, you may lost your writing';
  });

  $('#btn_submit').click(function(){ btn_submit_pressed=true; });

</script>

You could introduce a variable: 您可以引入一个变量:

var isSaved = true;  // at the beginning there's nothing unsaved

Whenever the user updates something, set: 每当用户更新内容时,请设置:

isSaved = false;

When the user clicks submit, set: 当用户单击提交时,设置:

isSaved = true;

Then in beforeunload , just check: 然后在beforeunload ,只需检查:

if(!isSaved) {

Pawar's code is a big idea. Pawar的代码是个好主意。 to compress and generize the code, #btn_submit changed to "input submit". 为了压缩并生成代码,#btn_submit更改为“输入提交”。

message in return is appear only IE not firefox, chrome. 消息返回只显示IE,而不是Firefox,Chrome。

<script type="text/javascript"> 
  var btn_submit_pressed = true; 
  $(window).bind('beforeunload', function(){ 
  if ( btn_submit_pressed ) 
        return 'if you leave this page, chages will be lost.'; 
  }); 
  $('#btn_submit').click(function(){ btn_submit_pressed=false; }); 
</script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM