限制按钮提交js

/**
 * 扩展jQuery ajax
 */
//扩展load方法,增加timout处理
jQuery.fn.singleTimeLoad = function(url, params, callback) {
var selector, type, response, self = this, off = url.indexOf(" ");


if (off > -1) {
selector = jQuery.trim(url.slice(off));
url = url.slice(0, off);
}


if (jQuery.isFunction(params)) {
callback = params;
params = undefined;


} else if (params && typeof params === "object") {
type = "POST";
}
if (self.length > 0) {
//jQuery.singleLoading();
//console.info($(self).attr('id'));
jQuery.singleAjax( {
url : url,
type : type || "GET",
dataType : "html",
timeout : 190000,

data : params
} ).done( function( responseText ) {
response = arguments;


self.html( selector ?
jQuery( "<div>" ).append( jQuery.parseHTML( responseText ) ).find( selector ) :
responseText );
} ).always( callback && function( jqXHR, status ) {
//jQuery.singleRemove();
//console.info(status);
if(status=='timeout'){
var timeoutRespTxt = '<div align="center" style="margin-top: 13%;">'+
'<p style="color: red;font-size: 24px;">错误码:[10000-503]</p><br>' + 
'<p style="color: red;font-size: 24px;">连接/操作超时,请稍后刷新再试,谢谢。</p></div>';
self.html( selector ?
jQuery( "<div>" ).append( jQuery.parseHTML( timeoutRespTxt ) ).find( selector ) :
timeoutRespTxt );
}else{
self.each( function() {
callback.apply( this, response || [ jqXHR.responseText, status, jqXHR ] );
} );
}
} );
}
return this;
}
//扩展load方法,增加loading
jQuery.fn.singleLoad = function(url, params, callback) {
var selector, type, response, self = this, off = url.indexOf(" ");


if (off > -1) {
selector = jQuery.trim(url.slice(off));
url = url.slice(0, off);
}


if (jQuery.isFunction(params)) {
callback = params;
params = undefined;


} else if (params && typeof params === "object") {
type = "POST";
}
if (self.length > 0) {
//jQuery.singleLoading();
jQuery.singleAjax( {
url : url,
type : type || "GET",
dataType : "html",
data : params
} ).done( function( responseText ) {
response = arguments;


self.html( selector ?
jQuery( "<div>" ).append( jQuery.parseHTML( responseText ) ).find( selector ) :
responseText );
} ).always( callback && function( jqXHR, status ) {
//jQuery.singleRemove();
self.each( function() {
callback.apply( this, response || [ jqXHR.responseText, status, jqXHR ] );
} );
} );
}
return this;
}


//扩展ajaxSubmit方法
jQuery.fn.singleAjaxSubmit = function(options){
var self = this;
    if (typeof options == 'function') {
        options = { success: options };
    }
    else if ( options === undefined ) {
        options = {};
    }
    
    var oldComplete = options.complete;
    options.complete = removeDeferred;
    if (oldComplete && typeof oldComplete == 'function'){
    options.complete = function(){
    removeDeferred();
    oldComplete();
    }
    }


    method = options.type || self.attr2('method');
    action = options.url  || self.attr2('action');
    
var href = window.location.href;
var sid = getSid( href, action );

var deferred = jQuery._single_r[sid];
if (deferred){ 
if (deferred.state()=='pending'){ 
jQuery.singlePending();
return self;
}
}

jQuery.singleLoading();//显示圈圈
deferred = jQuery._single_r[sid] = $.Deferred();//锁住请求

return self.ajaxSubmit(options);

function removeDeferred(){
try{
jQuery._single_r[sid] = null;
jQuery.singleRemove();
}catch(err){
}
}

function getSid(href, uri){
return hashcode( href + "|" + uri);
}

function hashcode(str) {
 var hash = 0, i, chr, len;
 if (str.length === 0) return hash;
 for (i = 0, len = str.length; i < len; i++) {
   chr   = str.charCodeAt(i);
   hash  = ((hash << 5) - hash) + chr;
   hash |= 0; // Convert to 32bit integer
 }
 return hash;
}
}


//扩展jQuery ajax,同一时间同一个页面(根据请求url)只允许一次提交.
jQuery.extend({
/**
* 重复提交时提示
*/
singlePending : function pending(){
alert("正在提交,请稍后.");
},
singleLoading : function(){
//正在提交
if ( $('.dd_popup_wrap').length == 0 ){ 
var img = $('#staticsCtx').val() + '/images/ajax_loading.gif';
$('body').append('<div class="dd_popup_wrap"><img class="dd_load" src="'+img+'"></div>');
}
},
singleRemove : function(){
$('.dd_popup_wrap').remove();
},
singleAjax : function( url, options ){
var _this = this;
if ( typeof url === "object" ) {
options = url;
url = undefined;
}

_this._single_r = (_this._single_r ? _this._single_r : {});

var surl= (url ? url : options.url);
var href = window.location.href;
var sid = getSid( href, surl);

var deferred = _this._single_r[sid];
if (deferred){ 
if (deferred.state()=='pending'){ 
$.singlePending();
return deferred;
}
}

jQuery.singleLoading();//显示圈圈
deferred = _this._single_r[sid] = $.Deferred();//锁住请求
deferred = _this._single_r[sid] = $.ajax( url, options );//请求

//正在提交
deferred.always(function(){ 
jQuery.singleRemove();
});

deferred.always(function(){
_this._single_r[sid] = null;
});

return deferred;

function getSid(href, uri){
return hashcode( href + "|" + uri);
}

function hashcode(str) {
 var hash = 0, i, chr, len;
 if (str.length === 0) return hash;
 for (i = 0, len = str.length; i < len; i++) {
   chr   = str.charCodeAt(i);
   hash  = ((hash << 5) - hash) + chr;
   hash |= 0; // Convert to 32bit integer
 }
 return hash;
}
},

singlePost : function( url, data, callback, type ){
if ( $.isFunction( data ) ) {
type = type || callback;
callback = data;
data = undefined;
}

return $.singleAjax( $.extend( {
url: url,
type: 'post',
dataType: type,
data: data,
success: callback
}, $.isPlainObject( url ) && url ) );
}
});
//关闭AJAX相应的缓存方法
jQuery.ajaxSetup ({ 
    cache: false  
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值