简体   繁体   English

如何使用jQuery MaskedInput 1.3正确设置小数掩码

[英]How to properly set up a decimal mask using jQuery MaskedInput 1.3

I am using jQuery 1.7.1 and jQuery maskedinput 1.3 in my ASP.NET MVC 3 app. 我在我的ASP.NET MVC 3应用程序中使用jQuery 1.7.1jQuery maskedinput 1.3

How would I set up a masked item that accepts a decimal value (SQL decimal 10,2). 如何设置一个接受十进制值的掩码项(SQL十进制数10,2)。 What I have works but it looks horrible: 我有什么作品,但它看起来很可怕:

$('#AnnualIncome').mask('9?9999999.99', { placeholder: ' ' });

When I go to my page and click on the textbox then there is space for numeric values with a . 当我转到我的页面并单击文本框时,有一个数字值空间。 further on for the decimal part. 进一步小数部分。 This looks horrible and it doesn't seem to work well. 这看起来很糟糕,似乎效果不佳。 I will type in 9 for the left part of the decimal and then 99 for the right part of the decimal. 我将在小数的左半部分输入9,然后在小数的右边部分输入99。 So if the textbox still has the focus then it looks something like this: 因此,如果文本框仍然具有焦点,那么它看起来像这样:

9___________.99

I don't want it like this, I want the user to be able to type in a decimal anytime, I want something like: 我不希望它像这样,我希望用户能够随时输入小数,我想要类似的东西:

9.99

Also, if the textbox looses focus, then it now looks like: 此外,如果文本框失去焦点,那么它现在看起来像:

999

I need it to display: 我需要它来显示:

9.99

Can someone please help me set this up properly? 有人可以帮我正确设置吗?

The Masked Input jQuery Plugin is meant for fixed width input, like formatting phone numbers. Masked Input jQuery插件用于固定宽度输入,如格式化电话号码。 Have a look at eg the numeric Plugin or the autoNumeric Plugin instead. 请查看例如数字插件autoNumeric插件

The Masked Input jQuery Plugin can be configured to support variable width decimal places. Masked Input jQuery插件可以配置为支持可变宽度小数位。

Firstly add a mask definition that supports your local decimal place separator or numeric characters: 首先添加一个支持本地小数位分隔符或数字字符的掩码定义:

$.mask.definitions['d'] = '[0-9.]';

Then define the mask: 然后定义掩码:

$('#AnnualIncome').mask('9?dddddddd', ' ');

Decimal places are optional and you cannot control the number of decimal places but still useful in certain scenarios and saves adding another plugin. 小数位是可选的,您无法控制小数位数,但在某些情况下仍然有用,并保存添加另一个插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM