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

后端 未结 5 1303
野趣味
野趣味 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 20:06

    you can put a variable in the global namespace, perhaps named ajaxLock and toggle it on when AJAX starts and off when the response comes. Then check it before allowing submit.

    something like

    var ajaxLock = 1;
    $('input#city').on('blur', function() {
    $.ajax({
        url: 'get/something?param=val',
        success: function(response) {
            $('input:hidden[name="something"]').val(response);
            ajaxLock = 0;
        }
        });
    });
    

提交回复
热议问题