上传文件 隐藏input type="file",用text显示

本文探讨了HTML中文件上传组件的设计与实现细节,通过JavaScript增强用户体验,包括隐藏功能的运用,如display:none与visible:hidden的区别,以及如何通过事件监听实现文件选择与预览功能。

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




html:


    <label for="upload_file" class="col-sm-2 control-label">上传文件</label>

    <div class="col-sm-4">
        <input type="file" class="form-control" id="upload_file" name="upload_file"
               style="display: none;" onchange="change();">
        <input type="text" class="form-control" id="upload_file_tmp" name="upload_file_tmp"
               readonly="readonly" onclick="upload_file.click(); ">
    </div>
    <div class="col-sm-2">
        <button type="button" class="btn btn-primary" id="select_file"
                onclick="upload_file.click();">浏览
        </button>
    </div>



js:

function change(){
    document.getElementById("upload_file_tmp").value=document.getElementById("upload_file").value;
}

另,

隐藏功能:

display:none 不保留物理空间,彻底消失

visible:hidden 占空间,但看不到




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值