【问题标题】:Ajax method working twice in a button clickAjax 方法在单击按钮时工作两次
【发布时间】:2016-02-10 10:59:07
【问题描述】:

文件上传功能作用于上传按钮点击。功能

$("#fuPDFAdd").change(function () {})

单击“btnUploadAdd”按钮时,文件上传更改正在工作两次。 如何避免这种情况

<div id="divUploadFileAdd">
    <form enctype="multipart/form-data" id="frmUplaodFileAdd">
        @Html.AntiForgeryToken()
        <input id="fuPDFAdd" name="file" type="file" style = "display:none;"/> 
        <button class="" id="btnUploadAdd" type="button" onclick="test()">Upload</button>
        <label id="txtuploadedMsgAdd"> </label>
    </form>
</div>
$(document).ready(function () {
    $("#fuPDFAdd").change(function () {
        console.log("tst1");
        var file = this.files[0];
        fileName = file.name;
        size = file.size;
        type = file.type;
        if (type.toLowerCase() == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { //I just want pdf files and only want to show
            var formData = new FormData($('#frmUplaodFileAdd')[0]);
            $.ajax({
                url: "UploadFile",  //Server script to process data
                type: 'POST',
                async: false,
                xhr: function () {  // Custom XMLHttpRequest
                    var myXhr = $.ajaxSettings.xhr();
                    if (myXhr.upload) { // Check if upload property exists
                        myXhr.upload.addEventListener('progress',
                        progressHandlingFunction, false); // For handling the progress of the upload
                    }
                    return myXhr;
                },
                data: formData,
                //Options to tell jQuery not to process data or worry about content-type.
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    grdStaffAddition.PerformCallback({ transStatus: "New" });
                    ShowClientToastr('False', 'False', 'toast-bottom-right', 'True', 'success', 'Template migration completed' + data.result, 'CAM - Contract Staff');
                }
            });
        }
        else {
            ShowClientToastr('False', 'False', 'toast-bottom-right', 'True', 'error', 'Please select xls/xlsx file.', 'CAM - Contract Staff');
        }
    });
});

function test() {   
    $("#fuPDFAdd").click();
}

【问题讨论】:

  • $("#fuPDFAdd").click();("#fuPDFAdd").change(); ?
  • 您必须提供复制问题的简约样本

标签: javascript jquery ajax asp.net-ajax


【解决方案1】:

这样的事情会起作用,我确实将文件上传代码包装到一个单独的函数中,并让它从你的事件侦听器调用这个新函数。

function uploadTheFile() {
    console.log("tst1");
    var file = this.files[0];
    fileName = file.name;
    size = file.size;
    type = file.type;
    if (type.toLowerCase() == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") { //I just want pdf files and only want to show
        var formData = new FormData($('#frmUplaodFileAdd')[0]);
        $.ajax({
            url: "UploadFile", //Server script to process data
            type: 'POST',
            async: false,
            xhr: function() { // Custom XMLHttpRequest
                var myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) { // Check if upload property exists
                    myXhr.upload.addEventListener('progress',
                        progressHandlingFunction, false); // For handling the progress of the upload
                }
                return myXhr;
            },
            data: formData,
            //Options to tell jQuery not to process data or worry about content-type.
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
                grdStaffAddition.PerformCallback({
                    transStatus: "New"
                });
                ShowClientToastr('False', 'False', 'toast-bottom-right', 'True', 'success', 'Template migration completed' + data.result, 'CAM - Contract Staff');

            }
        });
    } else {
        ShowClientToastr('False', 'False', 'toast-bottom-right', 'True', 'error', 'Please select xls/xlsx file.', 'CAM - Contract Staff');

    }
}
$("#fuPDFAdd").change(function() {
    uploadTheFile();
});

function test() {
    uploadTheFile();
}

【讨论】:

  • 实际上代码function test() { $("#fuPDFAdd").click(); } 是用于文件上传对话框窗口打开而不是文件上传控制和上传按钮使用
猜你喜欢
  • 2019-12-10
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
相关资源
最近更新 更多