输入上的 v-model 通过其他脚本动态更改值? [英] v-model on input that dynamically changes value by other script ?
问题描述
我有两个输入用于存储来自谷歌地图脚本的 lat 和 lng,如果用户更改标记的位置 => 这两个输入获取用户啄的 lat 和 lng,所以我想获取这两个输入的值,我尝试了 v-model 但它没有用我实际上注意到只有当我通过键入或粘贴某些内容来更改这些输入的值时才会触发 v-model.有什么方法可以让我的 Vue 实例获得这些输入的值(比如 on-change)?
I have two input that store lat and lng from google map script, if the user changes the marker's position => these two inputs get the lat and lng that the user pecked, so I wanna get the value of these two inputs, I tried v-model but it didnt work I actually noticed that the v-model will be fired only if I changed the value of these input by typing or pasting something in. Is there a way that I can get the value of these inputs (like on-change) to my Vue instance?
推荐答案
要触发 Vue.js 在从外部脚本动态更改后读取输入的值,您必须调用:
To trigger Vue.js to read your input's value after it changes dynamically from an external script, you have to call:
document.querySelector('#element-id').dispatchEvent(new Event('input'))
在你的元素上.这适用于 和
元素.对于
元素,您必须调用:
on your element. This works on both <input>
and <textarea>
elements. For <select>
elements you would have to call:
document.querySelector('#element-id').dispatchEvent(new Event('change'))
这篇关于输入上的 v-model 通过其他脚本动态更改值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!