Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery从输入中获取文件名列表_Javascript_Jquery - Fatal编程技术网

Javascript 使用jquery从输入中获取文件名列表

Javascript 使用jquery从输入中获取文件名列表,javascript,jquery,Javascript,Jquery,我想从输入元素中获取选定的文件名。以下是html元素: <input id="camera-open" type="file" multiple> 但它只给了我一个文件名!有什么想法吗?试试看 //step by step $( '#camera-open' ).change( function( event ){ var fileList = Array.prototype.slice.call( event.target.files );

我想从输入元素中获取选定的文件名。以下是html元素:

 <input id="camera-open" type="file" multiple>
但它只给了我一个文件名!有什么想法吗?

试试看

//step by step
$( '#camera-open' ).change( function( event ){        
       var fileList = Array.prototype.slice.call( event.target.files );       
       var filePaths = fileList.map( function(file){ return file.name; });
       var onlyNames = filePaths.map( function(file){ return file.split('/').pop().split('\\').pop(); });
       console.log( onlyNames );
});

尝试初始化循环外部的变量

var names = [],
    fileName;
$('#camera-open').change(function(){        
  $('input[type=file]').each(function(){
    fileName = $(this).val().split('/').pop().split('\\').pop();
    names.push(fileName);
  });
});
console.log(names);

最初声明var names[],每次更改期间,它都会初始化为null
var names = [],
    fileName;
$('#camera-open').change(function(){        
  $('input[type=file]').each(function(){
    fileName = $(this).val().split('/').pop().split('\\').pop();
    names.push(fileName);
  });
});
console.log(names);