使用javascript显示文件名上传

display filename upload using javascript

本文关键字:文件名 显示文件 javascript 显示 使用      更新时间:2023-09-26

我想在上传文件时显示文件名。文件名应显示在span id="Filename"中

<p>
    <label style="width: 30%">Image</label>
    <label class="custom-file-upload" style="width: 23%"> 
        <input type="file" name="img_1" id="img_1"/> <i class="fa fa-cloud-upload"></i> Image 1
    </label>
    <span id="filename">none selected...</span> 
</p>

如果我使用这个js代码,文件名将显示在标签中

$("input[id='img_1']").change(function (e) {
var $this = $(this);
$this.next().html($this.val().split('''').pop());});

我该怎么做?

使用

$("#filename").html($this.val().split('''').pop());