dongyongyin5339 2015-10-07 20:03
浏览 25

我想动态地在jQuery函数中创建这个类似的HTML结构并附加到html标签id =“images”

I am uploading images and want to show separate progress bar for each image.
This kind of similar structure I want to create dynamically in jQuery. Here I m trying to create img element uploaded by input file and want to create html container classname 'preview' and put into already created #images tag by using jQuery. Will you please anyone fix it? so frustrated and thanks in advance to all intelligent brains here.

 <div id="images"> // already exist

 <div class="preview" id="rand"> 
 <img src="background/balance.jpg" id="rand"/>
 <div class="progressbar" id="rand">
 </div> // this is the preview container to create and put by dynamically in jQuery function

 </div>

this is my jQuery function.

   function handleFile(files) {    
   for (var i = 0; i < files.length; i++) {
   var file = files[i];  

   var fileReader=new FileReader();  

   var rand = Math.floor((Math.random()*100000)+3);  /* this is created for assign id attribute to div and img tag to differentiate each other */

   var imageElem=document.createElement("img");  

      imageElem.id=rand;

      var div = document.createElement('div');

      div.className = 'preview';    

      div.id = rand;

      var progress = document.createElement('div');

      progress.className='progressbar';

      progress.id=rand;

      fileReader.onload = (function(img) 

  {                             
    return function(e) 
  { 

        img.src = e.target.result;   

   };

   }) (imageElem);  

      /* here I am trying to create that structure */ 

           document.getElementById("images").appendChild(imageElem); /* and need to append here */ 

           fileReader.readAsDataURL(file);  

        //   document.getElementById("areatext").style.display = "none";

           document.getElementById("submit1").style.display = "block";
           document.getElementById("submit2").style.display = "block";

            uploadFile(file,rand);  
   }  

   }
    function uploadFile(file,rand) {  

    var xhr = new Array();

    xhr[rand] = new XMLHttpRequest();  

    var formData = new FormData();  

    formData.append('myFile[]',file);  

    xhr[rand].upload.addEventListener("progress", function(e) {  

    if (e.lengthComputable) {  

    $(".progressbar[id='"+rand+"']").css("width",(e.loaded / e.total) * 100 + "%");

   }  

   }, false);  

   xhr[rand].open("POST", "drop.php");  

   xhr[rand].overrideMimeType('text/plain; charset=x-user-defined-binary');  

   xhr[rand].send(formData);  

   }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题