jqurey 获取 form 表单中所有数据,支持多维数组
2016-03-11 16:24
375 查看
jqurey 获取 form 表单中所有数据,支持多维数组
jQuery.formHelper = {
status : true,
rules : null,
tagName : '',
error : function (msg)
{
console.log(msg);
jQuery.formHelper.status = false;
return false;
},
success : function ()
{
jQuery.formHelper.status = true;
return true;
},
regRul : {
"require" : /.+/,
"email" : /^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/,
"url" : /^http:\\/\\/[A-Za-z0-9]+\\.[A-Za-z0-9]+[\\/=\\?%\\-&_~`@[\\]\\":+!]*([^<>\\"\\"])*$/,
"currency": /^\\d+(\\.\\d+)?$/,
"number" : /^\\d+$/,
"zip" : /^[1-9]\\d{5}$/,
"integer" : /^[-\\+]?\\d+$/,
"double" : /^[-\\+]?\\d+(\\.\\d+)?$/,
"english" : /^[A-Za-z]+$/
},
otherRul :
4000
{
">" : function (val, point, msg)
{
return (val > point) ? jQuery.formHelper.success() : jQuery.formHelper.error(msg);
},
'in' : function (val, point, msg)
{
return ($.inArray(val, point) != -1) ? jQuery.formHelper.success() : jQuery.formHelper.error(msg);
},
'==' : function (val, point, msg)
{
return (val == point) ? jQuery.formHelper.success() : jQuery.formHelper.error(msg);
},
'remote': function (val, point, msg)
{
$.ajax({
type : "post",
dataType: "json",
url : point[0],
data : jQuery.formHelper.tagName + '=' + val,
success : point[1]
});
}
},
getClass : function (object)
{
return Object.prototype.toString.call(object).match(/^\\[object\\s(.*)\\]$/)[1];
},
getFormData: function (id)
{
var _url = $('#' + id).serialize();
var fixStr = function (str)
{
return decodeURIComponent(str.replace(/\\+/g, '%20'));
};
var act1 = function (str)
{
str.replace(reg1, function (a, b, c)
{
arg[b] = (typeof c == 'undefined' ? '' : c);
});
};
var act2 = function (str)
{
str.replace(reg2, function (a, b, c, d)
{
arg[b] = arg[b] || {};
arg[b][c] = (typeof d == 'undefined' ? '' : d);
});
};
var act3 = function (str)
{
str.replace(reg3, function (a, b, c, d, e)
{
arg[b] = arg[b] || {};
arg[b][c] = arg[b][c] || {};
arg[b][c][d] = (typeof e == 'undefined' ? '' : e);
});
};
var url = f
deff
ixStr(_url).replace(/'|"/g, '');
var arr = url.split('&');
var length = arr.length;
var reg1 = /(\\w+)=(\\w+)?/ig;
var reg2 = /(\\w+)\\[(\\w+)\\]=(\\w+)?/ig;
var reg3 = /(\\w+)\\[(\\w+)\\]\\[(\\w+)\\]=(\\w+)?/ig;
var arg = {};
for (var i = 0; i < length; i++)
{
var act = reg1.test(arr[i]) ? act1 : (reg2.test(arr[i]) ? act2 : (reg3.test(arr[i]) ? act3 : ''));
act(arr[i]);
}
return arg;
},
submit : function (formId, submitBtnId, action, success)
{
$('#' + submitBtnId).click(function ()
{
jQuery.formHelper.check(jQuery.formHelper.rules);
if (jQuery.formHelper.status)
{
$.ajax({
async : false,
type : "post",
dataType: "json",
url : action,
data : $('#' + formId).serialize(),
success : success
});
}
});
},
check : function (rules)
{
jQuery.formHelper.rules = rules;
$(rules).each(function ()
{
jQuery.formHelper.tagName =this[0];
var val = $('[name=' + this[0] + ']').val();
var rul = this;
jQuery.formHelper.done(rul, val);
});
},
validate : function (rules)
{
jQuery.formHelper.rules = rules;
$(rules).each(function ()
{
jQuery.formHelper.tagName =this[0];
$('[name=' + this[0] + ']').keyup({rul: this}, function (event)
{
var val = $(this).val();
var rul = event.data.rul;
jQuery.formHelper.done(rul, val);
});
});
return this;
},
done : function (rul, val)
{
if ($.inArray(rul[1], jQuery.formHelper.regRul) != -1)
{
return (jQuery.formHelper.regRul[(rul[1])].test(val)) ? jQuery.formHelper.success() : jQuery.formHelper.error(rul[2]);
}
else if (jQuery.formHelper.otherRul.hasOwnProperty(rul[1]))
{
return jQuery.formHelper.otherRul[rul[1]](val, rul[3], rul[2]);
}
else if (jQuery.formHelper.getClass(rul[1]) == 'RegExp')
{
return ((rul[1]).test(val)) ? jQuery.formHelper.success() : jQuery.formHelper.error(rul[2]);
}
else
{
return jQuery.formHelper.error('没有匹配规则');
}
}
};
JQurey.formHelper
jQuery.formHelper = { status : true,
rules : null,
tagName : '',
error : function (msg)
{
console.log(msg);
jQuery.formHelper.status = false;
return false;
},
success : function ()
{
jQuery.formHelper.status = true;
return true;
},
regRul : {
"require" : /.+/,
"email" : /^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$/,
"url" : /^http:\\/\\/[A-Za-z0-9]+\\.[A-Za-z0-9]+[\\/=\\?%\\-&_~`@[\\]\\":+!]*([^<>\\"\\"])*$/,
"currency": /^\\d+(\\.\\d+)?$/,
"number" : /^\\d+$/,
"zip" : /^[1-9]\\d{5}$/,
"integer" : /^[-\\+]?\\d+$/,
"double" : /^[-\\+]?\\d+(\\.\\d+)?$/,
"english" : /^[A-Za-z]+$/
},
otherRul :
4000
{
">" : function (val, point, msg)
{
return (val > point) ? jQuery.formHelper.success() : jQuery.formHelper.error(msg);
},
'in' : function (val, point, msg)
{
return ($.inArray(val, point) != -1) ? jQuery.formHelper.success() : jQuery.formHelper.error(msg);
},
'==' : function (val, point, msg)
{
return (val == point) ? jQuery.formHelper.success() : jQuery.formHelper.error(msg);
},
'remote': function (val, point, msg)
{
$.ajax({
type : "post",
dataType: "json",
url : point[0],
data : jQuery.formHelper.tagName + '=' + val,
success : point[1]
});
}
},
getClass : function (object)
{
return Object.prototype.toString.call(object).match(/^\\[object\\s(.*)\\]$/)[1];
},
getFormData: function (id)
{
var _url = $('#' + id).serialize();
var fixStr = function (str)
{
return decodeURIComponent(str.replace(/\\+/g, '%20'));
};
var act1 = function (str)
{
str.replace(reg1, function (a, b, c)
{
arg[b] = (typeof c == 'undefined' ? '' : c);
});
};
var act2 = function (str)
{
str.replace(reg2, function (a, b, c, d)
{
arg[b] = arg[b] || {};
arg[b][c] = (typeof d == 'undefined' ? '' : d);
});
};
var act3 = function (str)
{
str.replace(reg3, function (a, b, c, d, e)
{
arg[b] = arg[b] || {};
arg[b][c] = arg[b][c] || {};
arg[b][c][d] = (typeof e == 'undefined' ? '' : e);
});
};
var url = f
deff
ixStr(_url).replace(/'|"/g, '');
var arr = url.split('&');
var length = arr.length;
var reg1 = /(\\w+)=(\\w+)?/ig;
var reg2 = /(\\w+)\\[(\\w+)\\]=(\\w+)?/ig;
var reg3 = /(\\w+)\\[(\\w+)\\]\\[(\\w+)\\]=(\\w+)?/ig;
var arg = {};
for (var i = 0; i < length; i++)
{
var act = reg1.test(arr[i]) ? act1 : (reg2.test(arr[i]) ? act2 : (reg3.test(arr[i]) ? act3 : ''));
act(arr[i]);
}
return arg;
},
submit : function (formId, submitBtnId, action, success)
{
$('#' + submitBtnId).click(function ()
{
jQuery.formHelper.check(jQuery.formHelper.rules);
if (jQuery.formHelper.status)
{
$.ajax({
async : false,
type : "post",
dataType: "json",
url : action,
data : $('#' + formId).serialize(),
success : success
});
}
});
},
check : function (rules)
{
jQuery.formHelper.rules = rules;
$(rules).each(function ()
{
jQuery.formHelper.tagName =this[0];
var val = $('[name=' + this[0] + ']').val();
var rul = this;
jQuery.formHelper.done(rul, val);
});
},
validate : function (rules)
{
jQuery.formHelper.rules = rules;
$(rules).each(function ()
{
jQuery.formHelper.tagName =this[0];
$('[name=' + this[0] + ']').keyup({rul: this}, function (event)
{
var val = $(this).val();
var rul = event.data.rul;
jQuery.formHelper.done(rul, val);
});
});
return this;
},
done : function (rul, val)
{
if ($.inArray(rul[1], jQuery.formHelper.regRul) != -1)
{
return (jQuery.formHelper.regRul[(rul[1])].test(val)) ? jQuery.formHelper.success() : jQuery.formHelper.error(rul[2]);
}
else if (jQuery.formHelper.otherRul.hasOwnProperty(rul[1]))
{
return jQuery.formHelper.otherRul[rul[1]](val, rul[3], rul[2]);
}
else if (jQuery.formHelper.getClass(rul[1]) == 'RegExp')
{
return ((rul[1]).test(val)) ? jQuery.formHelper.success() : jQuery.formHelper.error(rul[2]);
}
else
{
return jQuery.formHelper.error('没有匹配规则');
}
}
};
相关文章推荐
- 5个常见可用性错误和解决方案
- 如何使用Visual Studio 2010在数据库中生成随机测试数据
- 对 jQuery 中 data 方法的误解分析
- C#实现windows form拷贝内容到剪贴板的方法
- extjs form textfield的隐藏方法
- C#实现在Form里面内嵌dos窗体的方法
- form身份验证通过后,只能用FormsAuthentication.RedirectFromLoginPage
- form.submit()不能提交表单的错误原因及解决方法
- Chrome Form多次提交表单问题的解决方法
- jquery load事件(callback/data)使用方法及注意事项
- JQurey Validation表单验证使用详解
- jquery.form.js实现将form提交转为ajax方式提交的方法
- jQuery中使用data()方法读取HTML5自定义属性data-*实例
- jquery中$(#form :input)与$(#form input)的区别
- jQuery form插件的使用之处理server返回的JSON, XML,HTML数据
- jQuery实现数秒后自动提交form的方法
- jquery text,radio,checkbox,select操作实现代码
- jQuery form 表单验证插件(fieldValue)校验表单
- JQUERY获取form表单值的代码
- Javascript实现关联数据(Linked Data)查询及注意细节