gpt4 book ai didi

javascript - 使用 jquery、html 和 css 显示多个文件上传的多个进度条

转载 作者:行者123 更新时间:2023-11-28 08:53:33 25 4
gpt4 key购买 nike

我使用一个浏览按钮选择要上传的多个文件,但所有上传的文件只显示 1 个进度条。请查看以下代码:

<!doctype html>
<html>
<head>
<title>Proper Title</title>
<style>
.a{
display:none;
}
</style>

</head>
<body>
<form id="myForm" method="post" enctype="multipart/form-data" ">
Files: <input type="file" id="files" name="files" multiple><br/>
<div id="selectedFiles"></div>
<progress class='a' max=100 value=10></progress>
<input type="submit" >
</form>
<script>
var selDiv = "";
document.addEventListener("DOMContentLoaded", init, false);
function init() {
document.querySelector('#files').addEventListener('change', handleFileSelect, false);
selDiv = document.querySelector("#selectedFiles");
}
function handleFileSelect(e) {
if (!e.target.files)
return;
selDiv.innerHTML = "";
var files = e.target.files;
var elements = document.getElementsByClassName('a');
for (var i = 0; i < files.length; i++) {
var f = files[i];
var p = elements[0];
selDiv.innerHTML += f.name + "<br>";
p.style.display = 'block';
setInterval(function () {
var a = p.value;
a = a + 10;
//document.write(a);
p.value = a;
}, 1500);
}
}
</script>
</body>
</html>

我必须为单独的文件上传显示单独的进度条。

最佳答案

试试这个:

<!doctype html>
<html>
<head>
<title>Proper Title</title>
<style>
.a{
display:none;
}

</style>

</head>

<body>

<form id="myForm" method="post" enctype="multipart/form-data">
Files: <input type="file" id="files" name="files" multiple><br/>
<div id="progress-wpr">
<div class="filename"></div>
<progress class='a' max=100 value=10></progress>
</div>
<input type="submit" >
</form>



<script>

var selDiv = "";

document.addEventListener("DOMContentLoaded", init, false);

function init() {
document.querySelector('#files').addEventListener('change', handleFileSelect, false);
}

function handleFileSelect(e) {

if(!e.target.files) return;

var files = e.target.files;

for(var i=1; i<files.length; i++) {
var progress = document.createElement("progress");
progress.setAttribute('class','a');
progress.setAttribute('max','100');
progress.setAttribute('value','100');
var filename = document.createElement("div");
filename.setAttribute('class','filename');
document.getElementById('progress-wpr').appendChild(filename);
document.getElementById('progress-wpr').appendChild(progress);
}

var elements = document.getElementsByClassName('a');
var filename = document.getElementsByClassName('filename');
for(var i=0; i<files.length; i++) {

var f = files[i];
var p=elements[i];

filename[i].innerHTML = f.name;
p.style.display='block';
setInterval(update_progress, 1500);

}

}
function update_progress(){
var elements = document.getElementsByClassName('a');
for(var i=0; i<elements.length; i++) {
var p=elements[i];
var a=p.value;
a=a+10;
p.value=a;
}
}
</script>

</body>
</html>

已更新

update_progress() 添加了函数

关于javascript - 使用 jquery、html 和 css 显示多个文件上传的多个进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27242259/

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