jqurey 获取 form 表单中所有数据,支持多维数组

来源:互联网 发布:深圳国税开票软件 编辑:程序博客网 时间:2024/06/07 03:39

jqurey 获取 form 表单中所有数据,支持多维数组


JQurey.formHelper

  1. jQuery.formHelper = { 
  2.     status     : true
  3.     rules      : null
  4.     tagName    : ''
  5.     error      : function (msg) 
  6.     { 
  7.         console.log(msg); 
  8.         jQuery.formHelper.status = false
  9.         return false
  10.     }, 
  11.     success    : function () 
  12.     { 
  13.         jQuery.formHelper.status = true
  14.         return true
  15.     }, 
  16.     regRul     : { 
  17.         "require" : /.+/
  18.         "email"   : /^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/
  19.         "url"     : /^http:\\/\\/[A-Za-z0-9]+\\.[A-Za-z0-9]+[\\/=\\?%\\-&_~`@[\\]\\":+!]*([^<>\\"\\"])*$/
  20.         "currency"/^\\d+(\\.\\d+)?$/
  21.         "number"  : /^\\d+$/
  22.         "zip"     : /^[1-9]\\d{5}$/
  23.         "integer" : /^[-\\+]?\\d+$/
  24.         "double"  : /^[-\\+]?\\d+(\\.\\d+)?$/
  25.         "english" : /^[A-Za-z]+$/ 
  26.     }, 
  27.     otherRul   : { 
  28.         ">"     : function (val, point, msg) 
  29.         { 
  30.             return (val > point) ? jQuery.formHelper.success() : jQuery.formHelper.error(msg); 
  31.         }, 
  32.         'in'    : function (val, point, msg) 
  33.         { 
  34.             return ($.inArray(val, point) != -1) ? jQuery.formHelper.success() : jQuery.formHelper.error(msg); 
  35.         }, 
  36.         '=='    : function (val, point, msg) 
  37.         { 
  38.             return (val == point) ? jQuery.formHelper.success() : jQuery.formHelper.error(msg); 
  39.         }, 
  40.         'remote'function (val, point, msg) 
  41.         { 
  42.             $.ajax({ 
  43.                 type    : "post"
  44.                 dataType: "json"
  45.                 url     : point[0], 
  46.                 data    : jQuery.formHelper.tagName + '=' + val, 
  47.                 success : point[1
  48.             }); 
  49.  
  50.         } 
  51.     }, 
  52.     getClass   : function (object) 
  53.     { 
  54.         return Object.prototype.toString.call(object).match(/^\\[object\\s(.*)\\]$/)[1]; 
  55.     }, 
  56.     getFormData: function (id) 
  57.     { 
  58.         var _url   = $('#' + id).serialize(); 
  59.         var fixStr = function (str) 
  60.         { 
  61.             return decodeURIComponent(str.replace(/\\+/g'%20')); 
  62.         }; 
  63.         var act1   = function (str) 
  64.         { 
  65.             str.replace(reg1, function (a, b, c) 
  66.             { 
  67.                 arg[b] = (typeof c == 'undefined' ? '' : c); 
  68.             }); 
  69.         }; 
  70.         var act2   = function (str) 
  71.         { 
  72.             str.replace(reg2, function (a, b, c, d) 
  73.             { 
  74.                 arg[b]    = arg[b] || {}; 
  75.                 arg[b][c] = (typeof d == 'undefined' ? '' : d); 
  76.             }); 
  77.         }; 
  78.         var act3   = function (str) 
  79.         { 
  80.             str.replace(reg3, function (a, b, c, d, e) 
  81.             { 
  82.                 arg[b]       = arg[b] || {}; 
  83.                 arg[b][c]    = arg[b][c] || {}; 
  84.                 arg[b][c][d] = (typeof e == 'undefined' ? '' : e); 
  85.             }); 
  86.         }; 
  87.         var url    = fixStr(_url).replace(/'|"/g''); 
  88.         var arr    = url.split('&'); 
  89.         var length = arr.length; 
  90.         var reg1   = /(\\w+)=(\\w+)?/ig
  91.         var reg2   = /(\\w+)\\[(\\w+)\\]=(\\w+)?/ig
  92.         var reg3   = /(\\w+)\\[(\\w+)\\]\\[(\\w+)\\]=(\\w+)?/ig
  93.         var arg    = {}; 
  94.         for (var i = 0; i < length; i++) 
  95.         { 
  96.             var act = reg1.test(arr[i]) ? act1 : (reg2.test(arr[i]) ? act2 : (reg3.test(arr[i]) ? act3 : '')); 
  97.             act(arr[i]); 
  98.         } 
  99.         return arg; 
  100.     }, 
  101.     submit     : function (formId, submitBtnId, action, success) 
  102.     { 
  103.         $('#' + submitBtnId).click(function () 
  104.         { 
  105.             jQuery.formHelper.check(jQuery.formHelper.rules); 
  106.             if (jQuery.formHelper.status) 
  107.             { 
  108.                 $.ajax({ 
  109.                     async   : false
  110.                     type    : "post"
  111.                     dataType: "json"
  112.                     url     : action, 
  113.                     data    : $('#' + formId).serialize(), 
  114.                     success : success 
  115.                 }); 
  116.             } 
  117.         }); 
  118.     }, 
  119.     check      : function (rules) 
  120.     { 
  121.         jQuery.formHelper.rules = rules; 
  122.         $(rules).each(function () 
  123.         { 
  124.             jQuery.formHelper.tagName =this[0]; 
  125.             var val = $('[name=' + this[0] + ']').val(); 
  126.             var rul = this
  127.             jQuery.formHelper.done(rul, val); 
  128.         }); 
  129.  
  130.     }, 
  131.     validate   : function (rules) 
  132.     { 
  133.         jQuery.formHelper.rules = rules; 
  134.         $(rules).each(function () 
  135.         { 
  136.             jQuery.formHelper.tagName =this[0]; 
  137.             $('[name=' + this[0] + ']').keyup({rul: this}, function (event) 
  138.             { 
  139.                 var val = $(this).val(); 
  140.                 var rul = event.data.rul; 
  141.                 jQuery.formHelper.done(rul, val); 
  142.             }); 
  143.         }); 
  144.         return this
  145.     }, 
  146.     done       : function (rul, val) 
  147.     { 
  148.         if ($.inArray(rul[1], jQuery.formHelper.regRul) != -1
  149.         { 
  150.             return (jQuery.formHelper.regRul[(rul[1])].test(val)) ? jQuery.formHelper.success() : jQuery.formHelper.error(rul[2]); 
  151.         } 
  152.         else if (jQuery.formHelper.otherRul.hasOwnProperty(rul[1])) 
  153.         { 
  154.             return jQuery.formHelper.otherRul[rul[1]](val, rul[3], rul[2]); 
  155.         } 
  156.         else if (jQuery.formHelper.getClass(rul[1]) == 'RegExp'
  157.         { 
  158.             return ((rul[1]).test(val)) ? jQuery.formHelper.success() : jQuery.formHelper.error(rul[2]); 
  159.         } 
  160.         else 
  161.         { 
  162.             return jQuery.formHelper.error('没有匹配规则'); 
  163.         } 
  164.     } 
  165. };



1 0
原创粉丝点击