jquery.formLocalStorage

自动保存表单数据

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/454871/1117300/jqueryformLocalStorage.js

  1. /*!
  2. * jQuery Form Local Storage Plugin v0.1
  3. * http://git.oschina.net/since1986/jquery_form_local_storage
  4. *
  5. * Copyright since1986
  6. * Released under the MIT license
  7. */
  8. (function($) {
  9. $.fn.formLocalStorage = function(options) {
  10. var form_selector = this.selector;
  11. var input_selector = form_selector + " :text, " + form_selector + " :checkbox, " + form_selector + " :radio, " + form_selector + " select, " + form_selector + " textarea, " + form_selector + " [type='date']";
  12. if(options.debug){ console.debug(this); }
  13. //插件选项
  14. var options = $.extend({
  15. storage_name_perfix : ( this.context.URL + form_selector + "@" ), //暂存的命名前缀
  16. storage_dom_css : {"font-style":"oblique"}, //暂存内容的css(用于区分原始内容与暂存内容)
  17. load_ready_callback : function(){}, //暂存内容加载完毕回调
  18. save_ready_callback : function(){}, //暂存内容保存完毕回调
  19. remove_ready_callback : function(){}, //暂存内容删除完毕回调
  20. debug : false, //调试模式
  21. },
  22. options || {});
  23. if(options.debug){ console.debug("storage_name_perfix: " + options.storage_name_perfix); }
  24. //表单加载完毕后从localStorage中载入暂存的表单内容
  25. this.ready(function(){
  26. var storage_count = 0;
  27. $(input_selector).each(function(){
  28. var storage_key = options.storage_name_perfix + this.name;
  29. var storage_value = localStorage.getItem(storage_key);
  30. if(storage_value != undefined && storage_value != null){
  31. $(this).val(storage_value);
  32. $(this).css(options.storage_dom_css);
  33. if(options.debug){ console.debug("Load from localStorage [" + storage_key + " : " + storage_value + "]"); };
  34. storage_count++;
  35. }
  36. });
  37. if(storage_count > 0) { options.load_ready_callback(); }
  38. });
  39. //监控表单内容变化并存入localStorage FIXME 动态写入的内容监控不到
  40. this.ready(function(){
  41. $(input_selector).change(function(){
  42. if(this.value != undefined && this.value != null){
  43. var storage_key = options.storage_name_perfix + this.name;
  44. var storage_value = this.value;
  45. localStorage.setItem(storage_key, storage_value);
  46. $(this).css(options.storage_dom_css);
  47. if(options.debug){ console.debug("Save to localStorage [" + storage_key + " : " + storage_value + "]"); };
  48. }
  49. options.save_ready_callback();
  50. });
  51. });
  52. //表单提交时自动清空此表单所有暂存内容
  53. this.submit(function(){
  54. $(input_selector).each(function(){
  55. var storage_key = options.storage_name_perfix + this.name;
  56. localStorage.removeItem(storage_key);
  57. if(options.debug){ console.debug("Remove from localStorage [" + storage_key + "]"); };
  58. });
  59. options.remove_ready_callback();
  60. });
  61. return this;
  62. };
  63. })(jQuery);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址