2013-04-06 39 views
0

我有一個使用新的多屬性的上傳表單,我做了一個ajax上傳表單,使事情更加用戶友好。我的問題是即時通訊嘗試更新所有這些文件上傳和附加到一個div的百分比,而不是一個百分比被更新,他們都從最後一個文件更新。這是一些代碼。如何選擇預加元素

$('#File').change(function(event) { 
     for(I = 0; I < this.files.length; I++) 
     { 
      var Name = this.files[I].name; 
      var Size = this.files[I].size; 
      var Type = this.files[I].type; 

      $('#UploadContent').prepend('<div class="UploadLabel" style="width:60%;">'+Name+'</div><div class="UploadLabel UploadPercent" style="width:10%;">0%</div><div class="UploadLabel" style="width:15%;">N/A</div><div class="UploadLabel" style="width:15%;">'+Type+'</div>'); 
      var Data = new FormData(); 
      Data.append('File[]', this.files[I]); 
      var Request = new XMLHttpRequest(); 
      Request.upload.addEventListener('progress', function(event){ 
       if(event.lengthComputable) 
       { 
        var Percent = event.loaded/event.total; 
        var Progress = $('#UploadContent').find('.UploadPercent'); 
        $(Progress).text(Math.round(Percent * 100) + '%'); 
       } 
      }); 
      Request.upload.addEventListener('load', function(event) { 
     }); 

      Request.open('POST', '/Home/Upload/Upload.php'); 
      Request.setRequestHeader('Chache-Control', 'no-cache'); 
      Request.send(Data); 
      $('#UploadModal').fadeIn('fast'); 
     } 
    }); 

現在,你可以在進度聽衆看到我

var progress = $('#UploadContent').find('.UploadPercent'); 

如何將我選擇應該是正確更新文件。如果有人可以找到一種完全不同的方法來改變百分比,那也太棒了! - 謝謝!

+0

我瞭解你要選擇添附的元素?如果是這樣,你應該首先使用jquery var someElement = $(「你想要什麼元素」)來創建它,然後加上 – Givi 2013-04-06 03:10:56

+0

好主意!讓我嘗試一下。 – 2013-04-06 03:11:45

+0

你能寫一個答案我無法得到它的工作@ GiviKesanashvili – 2013-04-06 03:14:20

回答

1

當你前面加上,添加一個新的,具體的class(是的,你可以使用一個id,但我只是堅持class)到.UploadPercent元素:

$('#UploadContent').prepend('<div class="UploadLabel" style="width:60%;">'+Name+'</div><div class="UploadLabel UploadPercent UploadTarget' + I + '" style="width:10%;">0%</div><div class="UploadLabel" style="width:15%;">N/A</div><div class="UploadLabel" style="width:15%;">'+Type+'</div>'); 
// LOOK HERE----------------------------------------------------------------------------------------------------------------------^ HERE 

當你」重新定位,使用此:

var progress = $('#UploadContent').find('.UploadTarget' + I); 

因爲你需要的I值基於你在哪裏環路上是準確的,你需要使用閉包爲好。所以,你的代碼最終會看起來像:

$('#File').change(function(event) { 
    for(I = 0; I < this.files.length; I++) { 
     (function (I) { 
      // Your current code inside the for loop 
     })(I); 
    } 
}); 

雖然從上面的例子肯定是一種選擇,它可能更有送到只是存儲到新插入的元素的引用,而不必應付新classI,然後再使用它。

下面是最後的代碼我會使用:

http://jsfiddle.net/MeL7L/2/

$("#File").on("change", function (event) { 
    for (var i = 0; i < this.files.length; i++) { 
     (function (curFile, i) { 
      var Name = curFile.files[i].name; 
      var Size = curFile.files[i].size; 
      var Type = curFile.files[i].type; 

      var newEl = ""; 
      newEl += '<div class="UploadLabel" style="width:60%;">' + Name + '</div>'; 
      newEl += '<div class="UploadLabel UploadPercent" style="width:10%;">0%</div>'; 
      newEl += '<div class="UploadLabel" style="width:15%;">N/A</div>'; 
      newEl += '<div class="UploadLabel" style="width:15%;">' + Type + '</div>'; 
      newEl = $(newEl); 
      $("#UploadContent").prepend(newEl); 
      var Data = new FormData(); 
      Data.append("File[]", curFile.files[i]); 
      var Request = new XMLHttpRequest(); 
      Request.upload.addEventListener("progress", function (event){ 
       if (event.lengthComputable) { 
        var Percent = event.loaded/event.total; 
        var Progress = newEl.find(".UploadPercent"); 
        Progress.text(Math.round(Percent * 100) + "%"); 
       } 
      }); 
      Request.upload.addEventListener("load", function(event) {}); 

      Request.open("POST", "/Home/Upload/Upload.php"); 
      Request.setRequestHeader("Cache-Control", "no-cache"); 
      Request.send(Data); 
      $("#UploadModal").fadeIn("fast"); 
     })(this, i); 
    } 
}); 
+0

沒有奏效。 – 2013-04-06 03:08:20

+0

@MoussaHarajli對不起,我誤解了真正的問題。我剛剛更新了我的答案。我向'.UploadPercent'元素添加了一個額外的類 - '「.UploadTarget」+ I'。這樣,你可以找到它的特定元素 – Ian 2013-04-06 03:14:38

+0

不工作,因爲我不是一個全球性的,你試圖在函數中添加我。 – 2013-04-06 03:25:54