admin管理员组文章数量:1356908
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?
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?
Share Improve this question asked Oct 31, 2016 at 22:40 SiempaySiempay 1,0121 gold badge13 silver badges34 bronze badges 2- What mechanism is changing the inputs? – Roy J Commented Nov 1, 2016 at 0:15
- just a js script putting the values in the inputs when the marker of the map change position – Siempay Commented Nov 1, 2016 at 9:06
3 Answers
Reset to default 9To 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'))
You need to watch your two variables, and assign the value to the new one.
Watch your variables using watch properties:
HTML:
<div id="el">
<form>
<input v-model="foo">
<input v-model="bar">
</form>
<p>{{ foo }}</p>
<p>{{ bar }}</p>
</div>
JS:
new Vue({
el: "#el",
data: {
foo: '',
bar: ''
},
watch: {
foo: function(value) {
this.foo = value
},
bar: function(value) {
this.bar = value
}
}
});
本文标签: javascriptvmodel on input that dynamically changes value by other script Stack Overflow
版权声明:本文标题:javascript - v-model on input that dynamically changes value by other script ? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744057375a2583499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论