setup(){
const uploadInput = ref(null)
const clickUpload = (e) => {
if (uploadInput.value) {
console.log(uploadInput.value) # 每次触发都打印了 2 次
uploadInput.value.click()
}
}
const fileChange = (e) => {
console.log(e) # 第二次选择文件没有触发事件
}
}
<template>
<div class="container content">
<div class="upload-box" @click="clickUpload">
<input type="file" class="hidden" accept="image/*" ref="uploadInput" @change="fileChange">
</div>
</div>
</template>
1
rabbbit 2022-10-01 16:31:30 +08:00
同一个文件不会而二次触发 change 事件
|
2
fangwenxue OP |
3
rabbbit 2022-10-01 16:57:57 +08:00
|
4
rabbbit 2022-10-01 17:22:14 +08:00
研究了一下,1 楼的回复应该是错误的。我也不清楚为什么不会触发两次。
不过有一点可以确定的是,不要这样写。 现在的代码加个 setTimeout 进去直接就变成死循环了。 <template> <div class="container content"> <div class="upload-box" @click="clickUpload"> <input type="file" class="hidden" accept="image/*" ref="uploadInput" @change="fileChange"> </div> </div> </template> <script> import {ref} from 'vue' export default { setup(){ const uploadInput = ref(null) const clickUpload = (e) => { if (uploadInput.value) { console.log(uploadInput.value) setTimeout(() => { uploadInput.value.click() }) } } const fileChange = (e) => { console.log(e) } return { uploadInput,clickUpload,fileChange } } } </script> |
5
yunyuyuan 2022-10-01 18:00:10 +08:00
|