admin管理员组文章数量:1312655
I have an input field that gets pre-populated from my DB, but I won't want it to be pre-filled if the value is 0
Eg:
<input v-model="price" type="text">
If
data: {
price: 0
}
I don't want my input field to display 0, I know that I can do stuff like :disabled="price == 0"
but this will hide the whole input field, I just don't want to display the value if its 0
I have an input field that gets pre-populated from my DB, but I won't want it to be pre-filled if the value is 0
Eg:
<input v-model="price" type="text">
If
data: {
price: 0
}
I don't want my input field to display 0, I know that I can do stuff like :disabled="price == 0"
but this will hide the whole input field, I just don't want to display the value if its 0
- What about programatically changing the value from 0 to "" in the model? Would that work? – Borjante Commented Aug 29, 2017 at 10:59
-
you could try something like this:
price !== 0
– codejockie Commented Aug 29, 2017 at 11:07
2 Answers
Reset to default 6Simply change price to empty string when it's 0:
created() {
//since 0 is considered false it will put an empty string in case priceFromDB is 0
this.price = priceFromDB || ''
}
As I understood you want to always have empty input when value of price is 0
, even if user type It manually.If that's true, something like this should work for you:
<div id="app">
<input type="text" v-model="price" @keyup="trackChange">
</div>
JS:
const app = new Vue({
el: '#app',
data: {
price: 0
},
methods: {
trackChange() {
this.price = parseInt(this.price) === 0 ? null : this.price
}
},
created() {
this.trackChange()
}
})
Demo: http://jsbin./sihuyotuto/edit?html,js,output
本文标签: javascriptVueJS dont display input value if its 0Stack Overflow
版权声明:本文标题:javascript - VueJS dont display input value if its 0 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741878439a2402604.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论