javascript - JS 访问 forloop 中的每一个元素
问题描述
我希望for循环中的一个按钮可以独立运行相同的功能,以便在单击时单击隐藏,但单击后没有响应。
html for循环:
{% for order_item in orders %}
<a class="btn btn-warning" >Upload File</a>
<input type="file" id="img-file{{ forloop.counter }}" hidden="hidden"/>
<button type="button" onclick="function upload_img({{ forloop.counter }}){}">Upload</button>
{% endfor %}
js:
<script>
function upload_img(x) {
const realFileBtn = document.getElementById('img-file'+x);
realFileBtn.click()
}
</script>
解决方案
您需要在内部调用函数onclick
而不是定义。
onclick="upload_img({{ forloop.counter }})"
推荐阅读
- javascript - 更新firebase时如何让ejs自动重新渲染
- mysql - 如何将特定行/单元格的类型从 int 更改为 varchar
- android - 我安装了 android studio 3.1.2 并显示错误
- database - 在mongodb中查找日期之间的数据?
- python - 变异算法效率
- python - 管理不同环境的配置
- kubernetes - 无法连接到服务器:拨打 tcp 192.168.99.100:8443:getsockopt:没有到主机的路由
- r - 如何使用 dplyr 链中数据的第一行更改 col 名称
- algorithm - Kotlin - 按类别和子类别排序的列表
- python - 而不是写很多和如何以pythonic方式缩短你的代码