admin管理员组文章数量:1391985
I have an input element that bind with v-model to a item value, I want to limit the user input to just type numeric value on range between 0 to 10, I was tried this thing before(add @input and check the input value to keep it in range)
my code is like this:
<v-text-field @input="checkItem" v-model="item"></v-text-field>
checkItem(val) {
if(parseInt(val) < 1) {
this.item = 1;
} else if(parseInt(val) >10) {
this.item = 10;
}
}
Problem
after first time we type number out of range the function works great and keep it in range but when we type out of range number again the element didn't update because the new item value is the same as the old item value! to solve this I try to use forceUpdate and the $forceUpdate()
not work!!!
for example
if user type anything between range number into input, because it's in range everything is ok;
but if user type 0 or any number outside of range, on the first time item value change to 1 if value was under 1 but if again type any negative value because the item value last time was changed to 1 when we set it to 1 again nothing happening on the view and the element value was not updated.
The main question is how to force vue to update this input field value?
I have an input element that bind with v-model to a item value, I want to limit the user input to just type numeric value on range between 0 to 10, I was tried this thing before(add @input and check the input value to keep it in range)
my code is like this:
<v-text-field @input="checkItem" v-model="item"></v-text-field>
checkItem(val) {
if(parseInt(val) < 1) {
this.item = 1;
} else if(parseInt(val) >10) {
this.item = 10;
}
}
Problem
after first time we type number out of range the function works great and keep it in range but when we type out of range number again the element didn't update because the new item value is the same as the old item value! to solve this I try to use forceUpdate and the $forceUpdate()
not work!!!
for example
if user type anything between range number into input, because it's in range everything is ok;
but if user type 0 or any number outside of range, on the first time item value change to 1 if value was under 1 but if again type any negative value because the item value last time was changed to 1 when we set it to 1 again nothing happening on the view and the element value was not updated.
The main question is how to force vue to update this input field value?
Share Improve this question edited Jun 1, 2019 at 13:57 RonU 5,8043 gold badges20 silver badges13 bronze badges asked Feb 28, 2019 at 0:25 newvertexnewvertex 1805 silver badges16 bronze badges 2-
Have you tried watching for changes using a
watch
method rather than binding to input? Modifying the model data in an input hook is not intended usage ofv-model
– rh16 Commented Feb 28, 2019 at 0:32 - @rh16 if you mean use :value to bind item value from data object and change the item on the watch from input change, I have to say yes I was tried this before and got the same problem(the view part is not update because of the value is not changed) – newvertex Commented Feb 28, 2019 at 1:30
3 Answers
Reset to default 3 <div><input type="number" v-model="item"></input></div>
</template>
<script>
export default {
name: "ranges",
data() {
return {
item: Number,
error: String
};
},
watch: {
item(newVal, lastVal) {
if (newVal > 10) this.item = 10
if (newVal < 1) this.item = 1
}
}
};
</script>
Here using the watcher you can do that validation
The only way to force the reactivity when the the final result is always the same, is to re-render the ponent for it to reflect the changes by updating its key
.
Reference link here.
I have forked the sample Vue project from mdiaz00147, and modify into this, and I think it works as the author intended it to be.
Solution Code modified from mdiaz00147 's code snippet
<template>
<div>
<input :key="inputKey" v-model="item" @change="checkItem" />
</div>
</template>
<script>
export default {
name: "ranges",
data() {
return {
item: null,
inputKey: 0,
};
},
methods: {
checkItem() {
if (parseInt(this.item) < 1) {
this.item = 1;
} else if (parseInt(this.item) > 10) {
this.item = 10;
}
this.inputKey += 1;
},
},
};
</script>
I found a very simple workaround for me to be the following when validating. These are the steps:
- simply set the value to be the wrong un validated value first and only set it on blur or change (not input because it will auto-blur)
- then run your validation logic and update the data.item (this will now work since it knows it changed)
in code all you have to change:
checkItem(val) {
this.item = val; // add this
if(parseInt(val) < 1) {
this.item = 1;
} else if(parseInt(val) >10) {
this.item = 10;
}
}
Just make sure you only pass the value up or "use" the value after validation pletes, expect the value to be outside of your validation parameters in watches and putes.
本文标签: javascriptvuejs force update input element not workingStack Overflow
版权声明:本文标题:javascript - vue.js force update input element not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744699328a2620469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论