Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax in for loop increments variable i value to the total length
    primarykey
    data
    text
    <p>I am developing a ajax upload with progress bar, if the file input isn't multiple there are no problem but I want to develop a multiple ajax upload and for this reason I create a "for" loop until number of files that user has selected.</p> <p>When the function enters here the value of indice is the value of len variable, why does this happen?</p> <pre><code> myXhr.upload.addEventListener('progress', function(e) { progressHandlingFunction(e, '.prog'+(indice)) }, false); </code></pre> <p>The complete code:</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function(e){ $('#uploader').submit(function(){ var inpf = document.getElementById('files'); var len = inpf.files.length; //console.log(inpf, len);return false; for(var i=0; i&lt;len; i++){ var indice = i; $('div').append('&lt;progress class="prog'+i+'" value="0"&gt;&lt;/progress&gt;&lt;br /&gt;'); var formData = new FormData(); formData.append('image', inpf.files[i]); $.ajax({ url: 'upload1.php', //server script to process data type: 'POST', xhr: function() { // custom xhr var myXhr = $.ajaxSettings.xhr(); if(myXhr.upload){ // check if upload property exists myXhr.upload.addEventListener('progress', function(e) { progressHandlingFunction(e, '.prog'+(indice)) }, false); // for handling the progress of the upload } return myXhr; }, //Ajax events //beforeSend: beforeSendHandler, success: function(data){ completeHandler(data, '.prog'+i); }, //error: errorHandler, // Form data data: formData, //Options to tell JQuery not to process data or worry about content-type cache: false, contentType: false, processData: false, }); } return false; }); }); function progressHandlingFunction(e, klass){ if(e.lengthComputable){ $(klass).attr({value:e.loaded, max:e.total}); } } function completeHandler(data, klass){ $(klass).attr({value:0}); } &lt;/script&gt; </code></pre> <p>Here you have the jsFiddle with the example: <a href="http://jsfiddle.net/Pgq9s/" rel="nofollow">http://jsfiddle.net/Pgq9s/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload