我将元素id“btn_submit”分配给write_ok按钮。
我想要以下内容:
如果我按下write_ok按钮,
写,
否则(如果我不按write_ok按钮)
在页面退出之前做警告消息。
我不知道如何找出按下哪个按钮。
我发现了这一点,但我无法让它发挥作用:jQuery: how to get which button was clicked upon form submission?
<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>
答案 0 :(得分:1)
你可以做点什么,
<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>
答案 1 :(得分:0)
您可以引入一个变量:
var isSaved = true; // at the beginning there's nothing unsaved
每当用户更新某些内容时,请设置:
isSaved = false;
当用户点击提交时,请设置:
isSaved = true;
然后在beforeunload
中,检查:
if(!isSaved) {
答案 2 :(得分:0)
Pawar的代码是个大主意。 为了压缩和生成代码,#btn_submit更改为“输入提交”。
回复中的消息只出现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>