Answer the question
In order to leave comments, you need to log in
How to make a universal method for editing input on the fly?
It seems to be a primitive task by the standards of Javascript, but it doesn’t work on Vue.
We need a universal method that would rule the input value on the fly according to the necessary rules, for example, change a comma to a dot in a number or lead to an integer. To bind such a method to any desired input.
The problem is not to return the processed value, only by directly updating the specified variable. But do not follow the method for each input
Non-working option 1. Works only with models without nesting option 1
Non-working option 2. Changes only the value in the input without changing the model option 2
Please tell me how to do it right
Answer the question
In order to leave comments, you need to log in
Changes only the value in input without changing the model
v-model
listens to the input event, so addevent.target.dispatchEvent(new Event('input'))
The solution may be useful to someone.
The code is for either a Vuetify component, because @change.native required (directly on input tag doesn't work)
<div id="app">
<v-text-field
v-model="myModel"
label="Tine:"
@keyup="filterToFloat($event)"
@change.native="setToFloat($event)"
>
<span slot="append">час</span>
</v-text-field>
</div>
<script>
let app = new Vue({
el: '#app',
data: {
item: {
subitem: 0.25
}
},
methods: {
setToFloat: (event) => {
event.target.value = parseFloat(event.target.value)
event.target.dispatchEvent(new Event('input'))
},
filterToFloat: (event) => {
event.target.value = event.target.value.replace(',', '.').replace(new RegExp(/[^0-9.]/), '')
event.target.dispatchEvent(new Event('input'))
},
},
})
</script>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question