如果Ajax没有收到响应,如何使按钮不可点击

时间:2014-08-18 15:20:50

标签: javascript jquery ajax

我想要做的是,当用户点击按钮并且Ajax正在等待响应时,该按钮应该是不可点击的,直到Ajax收到响应。我怎么能这样做?

脚本:

$(document).ready(function () {

    $('#sendRestorePass').click(function (e) {
        e.preventDefault();

        var data = {};
        data.restoreEmail = $('#restoreEmail').val();
        data.mypass = $('#mypass').val();

        $.ajax({
            type: "POST",
            url: "view/restorePass.php",
            dataType: "JSON",
            data: data,
            cache: false,
            success: function (response) {

                if (Number(response) === 0) {
                    $("#dialog-confirm-restoreEmail-0").dialog("open");
                    $('#restoreEmail').val('');
                }
                if (Number(response) == 1) {
                    $("#dialog-confirm-restoreEmail-1").dialog("open");
                }
                if (Number(response) == 2) {
                    $("#dialog-confirm-restoreEmail-2").dialog("open");
                }
            }
        });
        return false;
    });

});

1 个答案:

答案 0 :(得分:4)

运行功能时,将按钮设置为禁用:

.attr('disabled','disabled');

当ajax完成时,设置:

.attr('disabled', false);