jQuery: Submit a form only if no AJAX is currently running on the page

后端 未结 5 1254
野趣味
野趣味 2021-01-13 19:44

When the city input field is blurred I get somnething via an ajax request and set that as the value of a hidden field in the same form that the city field resides in.

<
5条回答
  •  离开以前
    2021-01-13 19:58

    Use a lock variable like you suggested:

    $('input#city').on('blur', function() {
        window.AJAX_CURRENTLY_RUNNING_ON_PAGE = true;
        $.ajax({
            url: 'get/something?param=val',
            success: function(response) {
                $('input:hidden[name="something"]').val(response);
            },
            complete: function() { window.AJAX_CURRENTLY_RUNNING_ON_PAGE = false; }
        });
    });
    
    $('form#find-users').on('submit', function() {
        if(window.AJAX_CURRENTLY_RUNNING_ON_PAGE) {
            return;
        }
        //dostuff
    });
    

提交回复
热议问题