dragon8997474 2018-11-23 00:30
浏览 82
已采纳

怎么做“如果div类为0则从1开始追加”

I'm trying to make multiple upload images via ajax with XMLHttpRequest. Everything is okay. Images post successfully and data returns as expected. So returned images are appending to related div class. But every time i upload images one by one, div class start from zero.

With this code,before to send file(s) i'm creating progressbar div, after each image loaded successfully, image replace to loder.

for (var i = 0; i < input.files.length; i++) {
            var fileId = i;
        $('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' + 
            '<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
            '</div>');  
    }   

With this function, no matter how many files, im uploading them:

for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
            var fileId = i

            $('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' + 
                '<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
                '</div>');  

        }   

function uploadSingleFile(file, i) {    

        var fileId = i;
        var ajax = new XMLHttpRequest();
        //Progress Listener
        ajax.upload.onprogress = function (e) {
            var percent = (e.loaded / e.total) * 100;
            $('#progressbar_' + fileId).animate({"width": percent + "%"},800);
        };

        //Load Listener
        ajax.onreadystatechange = function (e) {
            if (this.readyState == 4 && this.status == 200) {
                var data = ajax.responseText;
                var rep = JSON.parse(data);

            $('#progressbar_' + fileId).css("width", "100%");
            setTimeout(function(){
                $('#progressbar_' + fileId).remove();
                $('.uploaded_photo_grid.'+ fileId).html('');
                $.each(file, function(){

                    if(rep.error !== ''){
                        $(".uploaded_photo_grid."+ fileId ).html(rep.name + ' yüklenemedi');
                    } else {
                        $(".uploaded_photo_grid."+ fileId ).html(
                            '<img class="previewItem" src="'+rep.fcontent+'" id="img_'+rep.id+'">');
                    }

                });                 
            },1500);
        }
    };

      Rest of code...
    }

Also i tried editing these lines of code. It increases the value of div for each file but just first image appears.

var zero = $('.uploaded_photo_grid.0').length;
    for (var i = 0; i < this.files.length; i++) { //Progress bar and status label's for each file genarate dynamically
                var fileId;
                if(zero == 0){
                    uploadSingleFile(this.files[i], i);
                    console.log(this.files[i]);
                    fileId = i;
                } else {
                    uploadSingleFile(this.files[i+1], i+1);
                    fileId = i+1;
                    console.log(this.files[i]);
                }

                $('.up_preview').append('<div class="uploaded_photo_grid '+ fileId +'">' + 
                    '<div class="pro_bar" id="progressbar_' + fileId + '" style="width:0%"></div>' + loader +
                    '</div>');  

            }

What should i do? Is there a way to do this better? I mean upload images without form via XML with progressbar for each file. I don't choose to use plugin, that's not for me.

  • 写回答

1条回答 默认 最新

  • dongping1012 2018-11-23 01:04
    关注

    The following is the problematic line.

    uploadSingleFile(this.files[i+1], i+1);
    

    You want the value for i passed in uploadSingleFile to be the total number of images uploaded incremented by one.

    One approach to do this is to sum $('.up_preview').children().length, i, and 1. 1 is added in the sum because the in the loop i starts at 0.

    // var zero = $('.uploaded_photo_grid.0').length;
    var numImages = $('.up_preview').children().length;
    
    if (numImages === 0) {
      //...
    } else {
      uploadSingleFile(this.files[i+1], numImages + i + 1);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题