javascript - 当我使用嵌套单击进行输入类型文件设计自定义时,为什么单击不起作用?
问题描述
我有输入类型文件,此单击在其默认设计时是工作文件,但是当我尝试使用另一个按钮对其进行自定义时,这不起作用。
我的代码:-
$('button').click(function () {
$('[name=photo_input]').change(function (e) {
var imagePath = e.target.files[0].name;
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button">Select Photo</button>
<input type="file" name="photo_input" style="display:none;">
谢谢你!
解决方案
基本上你需要触发按钮点击内的事件。有关触发器 API的更多信息
$('button').on('click',function () {
debugger;
$('[name=photo_input]').trigger('click');
});
$('[name=photo_input]').change(function (e) {
var imagePath = e.target.files[0].name;
alert(imagePath);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button">Select Photo</button>
<input type="file" name="photo_input" style="display:none;">
推荐阅读
- python - 将节点类放入链表类python
- javascript - 文本输入过快时,自动选择 (addRange) 方法无法正常工作
- javascript - 如何使我的 jQuery 幻灯片中的幻灯片平滑淡出?
- php - WordPress中的引导导航
- python - Python脚本检查谷歌驱动器(URL)中是否存在文档?
- python - 如何一次销毁特定的小部件?特金特
- python - 如何在 postgresql 的选择查询中使用存在条件?
- javascript - 不显示输入字段的日期值
- scala - 为什么我无法从 slick 官方文档中找到类 Table 的定义?
- django - AWS ec2 Nginx + Django 服务器在没有太多流量的情况下轻松达到 100% 的 CPU 利用率