gpt4 book ai didi

javascript - 删除选择的文件名以输入多个

转载 作者:行者123 更新时间:2023-11-30 14:16:29 25 4
gpt4 key购买 nike

目前我的代码与这里的代码相同:How to display selected file names before uploading multiple files in Struts2?

如何将文件列表转换为数组并在每个文件名旁边添加删除链接并在上传前将其删除?

谢谢。

最佳答案

我会使用递归函数来做到这一点。请参阅以下解决方案:

function updateList () {

var input = document.getElementById('file');
// Create list or array
var list = [];
for (var i = 0, len = input.files.length; i < len; ++i) {
list.push(input.files.item(i));
}
// Output file list
outputList(list);
}

function outputList (list) {

var output = document.getElementById('fileList');
while (output.hasChildNodes()) {
output.removeChild(output.lastChild);
}

var nodes = document.createElement('ul');
for (var i = 0, len = list.length; i < len; ++i) (function(i) {

var node = document.createElement('li');
var filename = document.createTextNode(list[i].name);

var removeLink = document.createElement('a');

removeLink.href = 'javascript:void(0)';
removeLink.innerHTML = 'Remove';
removeLink.onclick = function () {
// Remove item
list.splice(i, 1);
outputList(list);
}

node.appendChild(filename);
node.appendChild(removeLink);
nodes.appendChild(node);
})(i);

output.appendChild(nodes);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" name="file" id="file" multiple
onchange="javascript:updateList()" />
<br/>Selected files:
<div id="fileList"></div>

关于javascript - 删除选择的文件名以输入多个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53494013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com