【问题标题】:How to alert user that is leaving a page without saving the info [duplicate]如何在不保存信息的情况下提醒用户离开页面[重复]
【发布时间】:2014-05-22 00:10:56
【问题描述】:
我做了一个小表格,询问产品名称和产品# 我的问题是如何提醒用户他正在离开页面而不保存信息。
<form action="index.php" method="post">
<input type="text" name="pnumber" value="" placeholder="Product Number...."/>
<input type="text" name="pname" placeholder="Product Name...." /> <br>
<button id="save">save</button>
</form>
【问题讨论】:
标签:
javascript
jquery
css
html
【解决方案2】:
一种方法:检查字段是否为空
$(window).unload(function() {
var pnumber = $("input[name=pnumber]").val();
var pname = $("input[name=pname]").val();
if(pnumber==="" || pname==="")
{
alert("Please fill the form");
return false;
}
});
另一个:有一个全局标志来检查
var saved = false; // global variable
$("#save").click(function() {
saved = true;
});
$(window).unload(function() {
if(saved==false)
{
alert("Please fill the form");
return false;
}
});