using jQuery, you can us...">

How to get input value of multiple files when changing using jQuery?

To get the value of <input type="file" name="upload"> using jQuery, you can use $('input').val() .

However, using the same method to get the value <input type="file" name="upload[]" multiple> , returns only one file, not all of them.

Is there any other way to do this?

+4
source share
3 answers

try console in

 document.forms[index].elements[index].files 

cache loop values ​​through them

+2
source

I used the code from another post ...

 $('input[type=file]').val() 

.. and added the identifier of each input field of a specific file. The code below gets the path from the input, pulls out only the file name from it and displays this value in another field for two different file upload fields. If you had a lot of them, you can create a loop based on this code:

 $('#File1').change(function(){ // when you leave the File1 form field var filename1 = $('input[type=file][id=File1]').val().split('\\').pop(); // get just the filename $('#DL_Filename1').val(filename1); // and place it in the DL_Filename1 box }); // end change $('#File2').change(function(){ // when you leave the File2 form field var filename2 = $('input[type=file][id=File2]').val().split('\\').pop(); // get just the filename $('#DL_Filename2').val(filename2); // and place it in the DL_Filename2 box }); // end change 
+1
source

This is because when you use $('input').val() , it only returns the value of the first of these inputs. You need to go in cycles to receive values ​​of all of them. See the code below:

  var $uploads = $('input'); $uploads.each(function() { console.log($(this).val()); }); 
-1
source

Source: https://habr.com/ru/post/1390350/


All Articles