如果表单中 数据已更改,则向用户发出警报 jQuery脏

  • 源码大小:59.7KB
  • 所需积分:1积分
  • 源码编号:19JP-3459
  • 浏览次数:666次
  • 最后更新:2023年06月15日
  • 所属栏目:表单
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

jQuery Dirty插件检测HTML表单是否为“Dirty”,并提示用户在离开之前保存更改。

如何使用它:

1.要使用此插件,请包含jquery.dirty.js查询在jQuery库之后。

  1. <script src="/path/to/cdn/jquery.slim.min.js"></script>
  2. <script src="/path/to/dist/jquery.dirty.js"></script>

2.调用HTML表单上的函数,并在表单变为“脏”时触发一个函数。

  1. <form action="#" id="form">
  2. Form Fields Here
  3. </form>
  1. $("#form").dirty({
  2. onDirty: function(){
  3. alert("Dirty");
  4. }
  5. });

3.当页面上有未保存的更改时,防止用户导航离开

  1. $("#form").dirty({
  2. preventLeaving: true
  3. });

4.如果表单为“脏”,则在用户试图导航离开时自定义警告消息。

  1. $("#form").dirty({
  2. leavingMessage: "There are unsaved changes on this page which will be discarded if you continue."
  3. });

5.当表单获得“Clean”时触发函数。

  1. $("#form").dirty({
  2. onClean: function(){
  3. alert("Clean");
  4. }
  5. });

6.API方法。

  1. // detects if a form has been modified
  2. // returns true or false
  3. $("#form").dirty("isDirty");
  4.  
  5. // detects if a form is Clean
  6. // returns true or false
  7. $("#form").dirty("isClean");
  8.  
  9. // refreshes events
  10. $("#form").dirty("refreshEvents");
  11.  
  12. // resets the form
  13. $("#form").dirty("resetForm");
  14.  
  15. // sets the state to Clean
  16. $("#form").dirty("setAsClean");
  17.  
  18. // shows modified form fields
  19. $("#form").dirty("showDirtyFields");

更新日志:

2022-07-23

  • JS更新

2022-07-22

  • 为回调函数添加保护子句

v0.8.3 (2021-02-15)

  • 更新版本,包括一些依赖项更新和基础设施更改,以通过GitHub操作构建npm包

2020-09-20

  • 删除stopImmediatePropagation以防止核攻击其他订阅者

2020-04-13

  • 如果恢复了初始值,则将表单标记为干净

预览截图