简体   繁体   English

使用jQuery从动态添加的输入字段中上传文件

[英]Uploading files using jQuery from dynamically added input fields

I'm using this jQuery api to handle file uploads with a status bar. 我正在使用 jQuery API使用状态栏来处理文件上传。 I have the following (working) code: 我有以下(工作)代码:

$(".imageinput")
    .live("fileuploadadd", function(e, data) {
        data.dataType = "json";
    })
    .live("fileuploadsend", function(e, data) {
        data.dataType = "json";
        debugger;
        $(
            '<div id="paragraph"' + createNewParagraphID() + '" class="new paragraph image">' +
                '<div class="progressbar"></div>' +
            '</div>' +
            controlDiv + createNewParagraph()
        ).insertAfter($(this).parent());
    })
    .live("fileuploadprogressall", function(e, data) {
        data.dataType = "json";
        var bar = $(this).parent().next().children().css("width", parseInt(100 * data.loaded / data.total, 10) + "%");
    })
    .live("fileuploaddone", function(e, data) {
        data.dataType = "json";
        $.each(data.result, function(index, file) {
            console.log("done");
        });
    });

I need to use the code below in order to get a response about the upload status from the server. 我需要使用下面的代码,以便从服务器获取有关上传状态的响应。 Since there is a possibility to add elements dynamicly, I need to use the live method. 由于可以动态添加元素,因此我需要使用live方法。

$(".imageinput").fileupload({
    dataType: "json"
});

How can I set the datatype in this api using dynamicly added elements? 如何使用动态添加的元素在此api中设置数据类型?

I found the answer myself: 我自己找到了答案:

$(".imageinput").live("hover", function(e) {
    $(".imageinput").fileupload({
        dataType: "json",
        start: function(e) {
            $(
                '<div id="paragraph"' + createNewParagraphID() + '" class="new paragraph image">' +
                    '<div class="progressbar"></div>' +
                '</div>' +
                controlDiv + createNewParagraph()
            ).insertAfter($(this).parent());
        },
        progressall: function(e, data) {
            var bar = $(this).parent().next().children().css("width", parseInt(100 * data.loaded / data.total, 10) + "%");
        },
        done: function(e, data) {
            $.each(data.result, function(index, file) {
                console.log("done");
            })
        }
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM