[英]How can i set my v-text-field to not show negative numbers?
I need to exclude negative numbers from my <v-text-field>
is there any props or a way to do it?我需要从我的
<v-text-field>
中排除负数是否有任何道具或方法可以做到这一点?
Here is it:就这个:
<v-text-field
:value="0"
class="mr-0 pt-0"
hide-details
single-line
type="number"
style="max-width: 60px; "
min=0
v-model="portata.quantita_portata"
></v-text-field>
You code should work.您的代码应该可以工作。
It's working to me.它对我有用。
By the way, if it's not work then you can use oninput
顺便说一句,如果它不起作用,那么您可以使用
oninput
For your given example:对于您给定的示例:
<v-text-field
v-model="portata.quantita_portata"
type="number"
oninput="if(this.value < 0) this.value = 0;"
></v-text-field>
If it is a quantity field, round off the value just in case.如果是数量字段,请将值四舍五入以防万一。
<v-text-field
class="mr-0 pt-0"
hide-details
single-line
type="number"
style="max-width: 60px; "
v-model="portata.quantita_portata"
@change="changeQuantity"
></v-text-field>
And method:和方法:
changeQuantity (qty) {
const val = Math.round(Number(qty))
let quantity = val
if (val <= 0) {
quantity = 0
}
this.portata.quantita_portata = quantity
}
This might not be the optimal solution, but it solved my problem and may do yours as well.这可能不是最佳解决方案,但它解决了我的问题,也可能解决你的问题。 I personally used a key to re-render my element.
我个人使用了一个键来重新渲染我的元素。 If you do not use the key it still works, but when you change the range by v-text-field itself, it will ignore the condition and negative numbers are still typeable.
如果您不使用该键,它仍然可以工作,但是当您通过 v-text-field 本身更改范围时,它将忽略条件并且负数仍然是可输入的。 Here a piece of my code:
这是我的一段代码:
<v-text-field
v-model="form.storyboard_reject_count"
:label="$tr('storyboard_reject_count')"
:type="'number'"
@input="validateNegativeNumber('src_key',
'storyboard_reject_count')"
:key="src_key"
/>
validateNegativeNumber(key, value){
if(this.form[value] < 0){
this.form[value] = 0;
this[key]++;
return;
}
},
You can add watcher:您可以添加观察者:
watch: {
'portata.quantita_portata': {
handler: function (after, before) {
// Changes detected.
if(after < 0)
{
this.portata.quantita_portata = 0;
// this.portata.quantita_portata = before; // can do this
}
},
deep: true
}
}
min="0"
<v-text-field
type="number"
min="0"
></v-text-field>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.