【问题标题】:vuejs currency input mask keyboardvuejs 货币输入掩码键盘
【发布时间】:2023-03-17 03:23:02
【问题描述】:

我正在使用以下代码通过 vue js 输入价格,但它没有给出我想要的输出。 我想从键盘键(POINT)中取消。只有逗号会起作用。顺便说一句,我是你们中的新人,如果您能相应地帮助我,我会很高兴。

示例: 0,00 1.000,00

Code

Vue.component('my-currency-input', {
    template: `
        <div>
            <input type="text" v-model="formattedCurrencyValue" @blur="focusOut"/>
        </div>`,
    data: function() {
        return {
            currencyValue: 0,
            formattedCurrencyValue: "0.00"
        }
    },
    methods: {
        focusOut: function() {
            // Recalculate the currencyValue after ignoring "$" and "," in user input
            this.currencyValue = parseFloat(this.formattedCurrencyValue.replace(/[^\d\.]/g, ""))
            // Ensure that it is not NaN. If so, initialize it to zero.
            // This happens if user provides a blank input or non-numeric input like "abc"
            if (isNaN(this.currencyValue)) {
                this.currencyValue = 0
            }
                        // Format display value based on calculated currencyValue
            this.formattedCurrencyValue = this.currencyValue.toFixed(2).replace(/(\d)(?=(\d{3})+(?:\.\d+)?$)/g, "$1,")
        },
    }
});

new Vue({
    el: '#app'
});

【问题讨论】:

  • 您可以决定小数分隔符是什么,而忽略其他字符,过滤除数字和小数之外的所有字符,而不是通过阻止几个键来对抗用户输入(这可能被视为应用程序故障)字段输入失去焦点时的分隔符。这可以帮助你stackoverflow.com/a/48550430/3679111
  • 感谢您的评论,但我仍然没有找到我想要的。

标签: vue.js


【解决方案1】:

您可以通过keydown 事件取消密钥。检查罐头并防止事件发生。我在这里创建了一个示例:https://jsfiddle.net/4ejrfhyp/7/

【讨论】:

  • 感谢您的回答。但我没有得到我想要的结果。我想要示例:1.000,20 & 0,00
  • 但是现在您知道如何自己完成这项工作了。但也许你最好为此使用第三方组件。看看github.com/RobinHerbots/Inputmask,它可以做你想做的事。
  • 感谢您的评论,但我仍然没有找到我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
相关资源
最近更新 更多