【发布时间】:2023-03-17 03:23:02
【问题描述】:
我正在使用以下代码通过 vue js 输入价格,但它没有给出我想要的输出。 我想从键盘键(POINT)中取消。只有逗号会起作用。顺便说一句,我是你们中的新人,如果您能相应地帮助我,我会很高兴。
示例: 0,00 1.000,00
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