您的位置:首页 > Web前端 > JQuery

JQuery Ajax提交form工具类

2016-07-23 23:55 344 查看
<script type="text/javascript">
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [ o[this.name] ];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};

$.fn.ajaxForm = function(config){
var form = this;
var formJson = $(form).serializeObject();
if(config.data){
$.extend(config.data,formJson);
}

if(config.beforeSubmit && config.beforeSubmit()===false){
return this;
}

var options = {

success : function(data) {
if(config.success){
config.success(data);
}

if(config.resetForm && config.resetForm===true){
$(form)[0].reset()
}

if(config.clearForm && config.clearForm===true){
$(form)[0].reset()
}
},
error : function(data) {
if(config.error){
config.error(data);
}
}
}

var newConfig = $.extend({},config,options);

$.ajax(newConfig);
}

</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: