简体   繁体   English

如何将我的 v-text-field 设置为不显示负数?

[英]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.

相关问题 如何在 v-text-field 上显示图像? - How to show image on v-text-field? 如何使用 v-text-field 自动填充 - How can i autofill with v-text-field 我们如何在 nuxtjs 中设置 vuetify 的 v-text-field 的默认值? - How can we set a default value of a v-text-field of vuetify in nuxtjs? 如何将文本从 Vuetify 的 v-text-field 复制到剪贴板? - How can I copy text from Vuetify's v-text-field to clipboard? 如何调整官方 Vue 过滤器示例以使用 Vuetify 的`v-text-field` - How can I adapt the official Vue filter example to use Vuetify's `v-text-field` 显示选定的值<v-select>在一个<v-text-field> ? - Show selected value of <v-select> in a <v-text-field>? 如何在 v-text-field vuetify 中限制特殊字符但允许字母、数字和空格 - How to restrict special characters but allow alphabets, numbers and space in v-text-field vuetify 默认情况下为所有 v-text-field 概述的 Vuetify 集 - Vuetify set outlined for all v-text-field by default "<i>How to display dynamic placeholder text of<\/i>如何显示动态占位符文本<\/b><v-text-field><i>in Vuetify?<\/i>在 Vuetify 中?<\/b>" - How to display dynamic placeholder text of <v-text-field> in Vuetify? 如何在“v-text-field”组件的输入中定义文本的颜色? - How to define color of the text in the input of the "v-text-field" component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM