我如何才能强制这个jquery等待click事件完成

本文关键字:等待 jquery click 事件 | 更新日期: 2023-09-27 18:27:42

我使用的是Ajax File Upload,它重载了onClientUploadCompleteAll事件以及其他事件。我在点击jquery中的Upload按钮后执行导航,我想强制该函数等待onuploadcompleteall执行后再执行导航。现在,它正在导航到uploadcompleteall运行之前的下一页。有没有办法强迫它等待,或者在onuploadcompleteall事件中获取用户点击的内容?

<asp:AjaxFileUpload ID="AjaxFileUpload1" CssClass="0001.01" runat="server" AllowedFileTypes="jpg,jpeg,png,gif" OnUploadStart="AjaxFileUpload1_UploadStart" OnUploadComplete="AjaxFileUpload1_UploadComplete" OnUploadCompleteAll="AjaxFileUpload1_UploadCompleteAll" OnClientUploadComplete="onClientUploadComplete" OnClientUploadCompleteAll="onClientUploadCompleteAll" OnClientUploadStart="onClientUploadStart"></asp:AjaxFileUpload>

onClientUploadCompleteAll事件:

function onClientUploadCompleteAll(sender, e) { //stuff }

执行点击的文件上传事件:

function handleFileUpload(form) {
    number_of_plugin_instances_having_photos = 0;
    $('.upload-photos').map(function(i, el){
        if($('.uploaded-thumbnail', el).length > 0 || $('.uploaded-thumbnail-title', el).length > 0) {
            number_of_plugin_instances_having_photos++;
        }
    });
    if(number_of_plugin_instances_having_photos == 0) {
        form.submit();
    } else {
        //setTimeout(function(){
            number_of_plugin_instances_callbacks_uploaded = 0;
            $('.upload-photos').map(function(i, el){
                if($('.uploaded-thumbnail', el).length > 0 || $('.uploaded-thumbnail-title', el).length > 0) {
                    $('.ajax__fileupload_uploadbutton', el).trigger('click');
                }
            });
        //}, 1000);
    }
}

调用文件上传事件

$.ajax({
        type: "POST",
        url: loc + "/SubmitSections",
        data: dataValue,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json'
    })
    .done(function (data, status) {
        if (data.d != "success") {
            $("#modal-submitting").modal('hide');
            $(".error_message").text("Unable to submit the data entered.  Please try again.  Additional Details: " + data.d);
            $("#modal-error-occurred").modal('show');
        }
        else {
            handleFileUpload(form, true);
            //NEED TO WAIT BEFORE NAVIGATING OTHERWISE COMPLETE ALL DOES NOT EXECUTE
            document.location = "general-appearance.aspx";
        }
    })
    .fail(function (data, status) {
        if (data.d != "success") {
            $("#modal-submitting").modal('hide');
            $(".error_message").text("Unable to submit the data entered.  Please try again.  Additional Details: " + data.d);
            $("#modal-error-occurred").modal('show');
        }
        else {
            handleFileUpload(form, true);
            //NEED TO WAIT BEFORE NAVIGATING OTHERWISE COMPLETE ALL DOES NOT EXECUTE
            document.location = "general-appearance.aspx";
        }
    });//ajax call end

我如何才能强制这个jquery等待click事件完成

事实证明,我可以使用表单操作来获取用户选择的导航位置。在onClientUploadCompleteAll中,我放置了以下内容:

function onClientUploadCompleteAll(sender, e) {
    //I have more than one photo upload
    number_of_plugin_instances_callbacks_uploaded++;
    if(number_of_plugin_instances_callbacks_uploaded == number_of_plugin_instances_having_photos){
        var first_form = $('form')['0'];
        var nextPage = first_form.action;
        document.location = nextPage;
        //first_form.submit();
    }
}