(function ($) {
"use strict";
//异步提交表单
$.fn.ajaxForm = function(options, func){
if (typeof options == 'function') options = { success: options };
if (typeof func == 'function') options.success = func;
options = $.extend(true, {
submit: this.prop('data-submit') || false,
type: this.prop('method') || 'post',
url: this.prop('action') || '',
success: function(data){ if(data.status == 1){ location.reload(); }else{ alert(data.info); }},
cache: false,
data: this.serialize(),
dataType:'json',
timeout:3000,
error: function(XMLHttpRequest, textStatus, errorThrown){ $.log('[jquery.ajax.error] ', 'status:' +textStatus + '; info:' + errorThrown);}
},(options !== undefined)? options : {});
if(options.submit){
return this.each(function(){$.ajax(options); });
}else{
$(this).on('submit', function(){ $.ajax(options); return false; });
}
};
$(document).on('submit', '[data-plug="ajaxform"]', function(e){
e.preventDefault();
$(this).ajaxForm({submit:true});
});
})(jQuery); 最佳答案