简体   繁体   English

每当用户输入数据时,如何强制触发@change/@input 事件?

[英]How to force @change/@input event to fire whenever user inputs data?

I can use input DOM element to upload an image and pass its data to a function like so:我可以使用input DOM element上传图像并将其数据传递给 function,如下所示:

<input type="file" @change="myFunction"/>

but since the event is @change , it only fires when the image data changes.但由于事件是@change ,它仅在图像数据更改时触发。 When I want to upload the same image twice, the event doesn't fire.当我想两次上传同一张图片时,该事件不会触发。 I've tried using the @input event instead but it behaves exactly the same, not firing when the same image is uploaded.我尝试使用@input事件,但它的行为完全相同,在上传相同的图像时不会触发。

Is there any way to upload the same image twice?有没有办法两次上传同一张图片?

You can achieve that by attaching the @click event and then empty the target value event.currentTarget.value = ''您可以通过附加@click事件然后清空目标值event.currentTarget.value = ''实现

Live Demo :现场演示

 new Vue({ el: '#app', methods: { myClickFunction(e) { e.currentTarget.value = ''; }, myFunction() { console.log('call'); } } })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <input type="file" @click="myClickFunction" @change="myFunction"/> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javascript如何在粘贴事件处理程序中触发更改或输入事件 - javascript how to fire change or input event in paste event handler 以编程方式触发输入的更改事件 - Fire on change event of input programmatically 以编程方式更改输入值时如何触发 JQuery 更改事件? - How to fire JQuery change event when input value changed programmatically? 如何在jQuery中选择closet元素并将fire事件更改为文件输入? - How to select closet element in jQuery and fire change event to file input? 如何在jQuery中触发更改事件 - How to fire change event in jQuery 为什么“输入”事件不会在文件输入上触发,而“更改”事件会触发呢? - Why do 'input' events not fire on file inputs, but 'change' events do? 如何强制.change jquery事件而无需用户更改文本框中的文本? - how to force .change jquery event without user changing the text in textbox? 如何在mouseout事件完成之前强制将mouseover事件触发 - How to force mouseover event to fire before mouseout event completes 如何为javascript更改输入值触发事件 - how to fire event for javascript changed input value 如何使用 Enzyme 触发模糊事件以更改输入元素的值? - How do I fire a blur event to change the value of a an input element with Enzyme?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM