如何将上传文件的名称设置为输入类型:HTML中的javascript上的文本

问题描述 投票:0回答:1

我想将文件上传控件添加到我的html页面,如下所示。

<input class="form-control valid" id="rptScreenShot" type="text" style="float:left;" runat="server"
                               data-val="true" data-val-maxlength="The field Report Sample must be a string or array type with a maximum length of '600'." 
                               data-val-maxlength-max="600" value="" aria-invalid="false" />
<label class="btn btn-default btn-file">
    Browse <input class="form-control" id="upld_sample1" type="file" name="file_rptsample1" style="display: none;">
</label>

我无法做的是在用户浏览文件后捕获所选文件,并通过javascript或jquery将其设置为rptScreenShot的值。任何帮助,将不胜感激。

javascript jquery html file-upload html-input
1个回答
1
投票

类似下面的内容?

$("#upld_sample1").on("change", function(e){
  console.log("Fileinfo:", e.target.files[0])
  $("#rptScreenShot").val(e.target.files[0].name)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="form-control valid" id="rptScreenShot" type="text" style="float:left;" runat="server" data-val="true" data-val-maxlength="The field Report Sample must be a string or array type with a maximum length of '600'." data-val-maxlength-max="600"
  value="" aria-invalid="false" />
<label class="btn btn-default btn-file">
    Browse <input class="form-control" id="upld_sample1" type="file" name="file_rptsample1" style="display: none;">
</label>
© www.soinside.com 2019 - 2024. All rights reserved.