0

I have a textbox and I want it to show an alert when leaving the page with unsaved changes, except if you click the submit button. This works great, except it still alerts you if the submit button is pressed and I really have no idea how to fix it.

I have 2 files, my Set.js to take the actions of the BBcode, and my script.js

script.js

$(document).ready(function() {
    var saveClicked = false;

    $(window).bind('beforeunload', function() {
        if(y != $("#markItUp").val()) {
            if(!saveClicked) {          
                return 'You have unsaved changes!';
            }
        }
    });
});

Set.js

{
    name: 'Save',
    className:"miu-save",
    call: function() {
        $('form#edit').submit(); 
        saveClicked = true;
    }
},
4

3 回答 3

2

改变这个

{
    name: 'Save',
    className:"miu-save",
    call: function() {
        $('form#edit').submit(); 
        saveClicked = true;
    }
},

{
    name: 'Save',
    className:"miu-save",
    call: function() {
        saveClicked = true; // true is called first here if you notice and then submit
        $('form#edit').submit(); 
    }
},

这也是必要的

var saveClicked = false;
$(document).ready(function(){

在 document.ready 函数之外声明你 saveClicked var

于 2013-04-24T07:19:49.850 回答
1

把你的saveClicked外面document.ready。这样它将是全局的,然后只有一个人可以将其值设置为trueor false

所以你的脚本应该是这样的:

  var saveClicked = false;
  $(document).ready(function(){

$(window).bind('beforeunload', function(){
   if(y != $("#markItUp").val()){
     if(!saveClicked){           
   return 'You have unsaved changes!';
  }}
 });
});
于 2013-04-23T08:40:50.223 回答
0

我不确定这会起作用,但也许如果你从做开始

saveClicked = true;

然后

$('form#edit').submit();

这样就可以了。

于 2013-04-23T08:36:22.787 回答