2010-11-29 31 views
1

我一直在试图实现jquery.safetynet插件,但我一直在使用复选框和跨多个浏览器时遇到困难。 (阅读:它必须是ie6兼容)当字段脏了,多浏览器实现?

我已经决定放弃这个实现,即使我真的很喜欢它。

您是否有很好的脏字段检查建议,并提示: 导航离开,刷新,浏览器关闭?

谢谢!

回答

0

使用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; 
}