输入的 v-model 通过其他脚本动态更改值?

2022-01-25 00:00:00 javascript vue.js vue-component

我有两个输入存储来自谷歌地图脚本的 lat 和 lng,如果用户更改标记的位置 => 这两个输入得到用户啄的 lat 和 lng,所以我想得到这两个输入的值,我尝试了 v-model 但它不起作用我实际上注意到只有当我通过键入或粘贴某些内容来更改这些输入的值时才会触发 v-model.有没有一种方法可以将这些输入的值(如 on-change)传递给我的 Vue 实例?

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'))

在您的元素上.这适用于 <input><textarea> 元素.对于 <select> 元素,您必须调用:

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'))

相关文章