作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想将文件上传控件添加到我的 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/
我是一名优秀的程序员,十分优秀!