教你写jquery插件

浏览:532 发布日期:2015/07/22 分类:技术分享 关键字: jquery
(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);
最佳答案
评论( 相关
后面还有条评论,点击查看>>