Javascript 关于元素值更改的Vuejs事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34723680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vuejs event on change of element value?
提问by Stephan-v
I have an element that I want to watch for a change like this:
我有一个元素,我想观察这样的变化:
<span id="slider-value-upper" class="lower">50</span>
Is it possible to do this cleanly with vuejs? I tried looking in the docs but I could not find anything like this.
是否可以使用 vuejs 干净利落地做到这一点?我尝试查看文档,但找不到类似的内容。
I want to launch a custom event whenever '50' changes to something else with VueJs.
我想在 VueJs 将“50”更改为其他内容时启动自定义事件。
回答by Yerko Palma
Have you tried with watch?
In your case it would be something like this.
你试过watch吗?在你的情况下,它会是这样的。
template
模板
<div id="app">
{{ message }}
<span id="slider-value-upper" class="lower">{{myValue}}</span><br />
<input v-model="myValue">
</div>
js code
js代码
new Vue({
el: '#app',
data: {
message: 'Watch example',
myValue: 50
},
watch: {
'myValue': function(val, oldVal){
if (val < 50) {
this.message= 'value too low!';
}else{
this.message= 'value is ok';
}
}
}
})
check out the example
查看示例

