gpt4 book ai didi

javascript - 如何将上传文件的名称设置为输入类型:text on javascript in html

转载 作者:行者123 更新时间:2023-12-02 21:03:33 25 4
gpt4 key购买 nike

我想将文件上传控件添加到我的 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 的值。任何帮助将不胜感激。

最佳答案

像下面这样吗?

$("#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>

关于javascript - 如何将上传文件的名称设置为输入类型:text on javascript in html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61281141/

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