JS中Input上传文件获取文件名并显示在另一文本框中

本文介绍了一个简单的文件上传功能实现,支持多文件上传、文件名显示及文件删除操作。通过HTML和JavaScript结合,使得用户能够直观地看到已选择的文件,并可以方便地移除不需要的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能:
1.上传多个文件
2.获取文件名
3.显示在右边文本框中
4.带有删除功能
在这里插入图片描述

在这里插入代码片
```<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .rit{
            width:1000px;
            height:35px;
           /* background-color:#eeeeee;*/
            margin-left:100px;
            margin-top:4px;
           text-align: center;
        }
        .lft {
            height: 30px;
            width: 80px;
            background-color: cornflowerblue;
            font-size: 14px;
            line-height: 30px;
            cursor: pointer;
            display: inline-block;
            text-align: center;
            color: #fff;
            border-radius: 5px;
            border: 1px solid #666666;
            margin-left: 20px;
        }
    </style>
</head>
<body>
<div class="row cl">
    <div class="upload" style="margin-top: 80px">
        <div class="lft" style="float:left;margin-left: 50px;">
            <button class="load" "document.getElementById('project_name').click()">选择文件</button>
            <input type="file" id="project_name" multiple style="display: none" "showFiles(this)">
        </div>
        <div id="con" class="rit"></div>
    </div>
</div>
<script>
    /*显示文件名在另一控件*/
    var uploadfiles=new Array();
    function  showFiles(o) {
        var e=window.event;
        console.log(e);
        var files=e.target.files;
        if(files.length>0){
            for(var i=0;i<files.length;i++){
                    uploadfiles.push(files[i]);
                }
            }
        refreshDiv();
    }
    function  refreshDiv() {
        var condiv=document.getElementById("con");
        con.innerHTML="";
        for(var i=0;i<uploadfiles.length;i++){
            var div=document.createElement("div");
            div.style="display:in-line";

            var span=document.createElement("span");
            span.innerHTML=uploadfiles[i].name;
            span.style.color="black";

            var bt=document.createElement("button");
            bt.innerHTML="X";
            bt.style.color="red";
            bt.style.background="transparent";
            bt.style.margin="0 10px";
            bt.dataId=i;
            bt.function () {
                console.log(this.dataId);
                uploadfiles.splice(this.dataId,1);
                refreshDiv();
            }
            condiv.appendChild(span);
            condiv.appendChild(bt);
        }
    }
</script>
</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值