gpt4 book ai didi

javascript - jScript 确认当字段脏时导航离开,多浏览器实现?

转载 作者:行者123 更新时间:2023-11-28 10:25:54 25 4
gpt4 key购买 nike

我一直在尝试实现 jquery.safetynet插件,但我一直很难让它与复选框和跨多个浏览器一起工作。 (阅读:必须兼容ie6)

我决定放弃这个实现,尽管我真的很喜欢它。

对于脏字段检查和提示,您有好的建议吗:导航离开,刷新,浏览器关闭?

谢谢!

最佳答案

使用 window.onbeforeunload 在页面卸载时收到通知。要检查表单中是否有任何内容是脏的,您可以查询所有输入,选择并查看值是否与页面加载时不同。

我从网上复制粘贴此内容以检查表单中的任何值是否被修改

window.onbeforeunload = function(e) {
if (isFormModified(document.getElementById('form-id-goes-here'))) {
return e.returnValue = "You have unsaved data in your form";
}
}

function isFormModified(oForm)
{
var el, opt, hasDefault, i = 0, j;
while (el = oForm.elements[i++]) {
switch (el.type) {
case 'text' :
case 'textarea' :
case 'hidden' :
if (!/^\s*$/.test(el.value) && el.value != el.defaultValue) {
return true;
}
break;
case 'checkbox' :
case 'radio' :
if (el.checked != el.defaultChecked) {
return true;
}
break;
case 'select-one' :
case 'select-multiple' :
j = 0, hasDefault = false;
while (opt = el.options[j++]) {
if (opt.defaultSelected) {
hasDefault = true;
}
}
j = hasDefault ? 0 : 1;
while (opt = el.options[j++]) {
if (opt.selected != opt.defaultSelected) {
return true;
}
}

break;
}
}
return false;
}

关于javascript - jScript 确认当字段脏时导航离开,多浏览器实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4306645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com