繁体   English   中英

禁用和启用Ajax启停事件后提交的响应方法

[英]Responsive method to disable and enable submit upon Ajax start-stop event

我尝试了以下方法:

1)显示/隐藏

$('button').ajaxStart(function() {
 $(this).hide();
});
$('button').ajaxStop(function() {
 $(this).show();
});

2)可见/隐藏

$('button').ajaxStart(function() {
 $(this).css({visibility, 'hidden'});
});
$('button').ajaxStop(function() {
 $(this).css({visibility, 'visible'});
});

3)添加/删除课程

$('button').ajaxStart(function() {
 $(this).addClass('invisible');
});
$('button').ajaxStop(function() {
 $(this).removeClass('invisible');
});

4)启用/禁用

$('button').ajaxStart(function() {
 $(this).attr('disabled','disabled')
});
$('button').ajaxStop(function() {
 $(this).removeAttr('disabled');
});

到目前为止,仅Show / Hide方法具有足够的响应能力,可以在1秒内执行。 其余的时间在1-2秒之间,这很长。 但是,我希望按钮可以显示,但是由于一些明显的CSS更改而被禁用。 有没有更灵敏的方法来临时启用和禁用“提交”按钮?

尝试这个:

$("button").click(function() {

    // disable the submit button before the ajax call...
    $(this).attr('disabled','disabled');
    $.ajax({
        url: 'yoururl.html',
        data: {},
        type: 'post',
        success: function(response){
            // enable the submit button after ajax call success...
            $(this).removeAttr('disabled');
        },
        error: function(){
            // error process here
            $(this).removeAttr('disabled');
        },
        dataType: 'json'
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM