简体   繁体   English

输入显示文字的Javascript数字

[英]Javascript Number input with text displayed

For my website, I want a number input that includes a dollar sign after the number, ie when you type "30", it displays "30$" in the input field. 对于我的网站,我想要一个数字输入,该数字输入必须在数字后面包含一个美元符号,即当您键入“ 30”时,它在输入字段中显示“ 30 $”。 The user should not be allowed to write any non-numeric characters, and it should behave like a "normal" text/number input field otherwise. 不允许用户写任何非数字字符,否则,它的行为应类似于“常规”文本/数字输入字段。 (I don't need the spinner that comes with type="number" in an input field). (我不需要输入字段中带有type =“ number”的微调器)。

Is there anything like that, or is it not possible due to complexity? 有那样的东西吗,还是由于复杂性不可能吗? jQuery answers are welcome too. jQuery的答案也很受欢迎。

You can use the following plugin to only allow the numeric values in your text input. 您可以使用以下插件在文本输入中仅允许数字值。 As @charlietfl commented, the best practice is to use the sign outside of the input field. 正如@charlietfl所评论的,最佳实践是在输入字段之外使用符号。

http://www.texotela.co.uk/code/jquery/numeric/ http://www.texotela.co.uk/code/jquery/numeric/

This is the plugin. 这是插件。 After you link it in your HTML file, in document.ready function in Jquery you just get your element and call numeric() method. 在将其链接到HTML文件中之后,在Jquery的document.ready函数中,您只需获取元素并调用numeric()方法。

Check out the jquery.inputmask library: https://github.com/RobinHerbots/jquery.inputmask 签出jquery.inputmask库: https : //github.com/RobinHerbots/jquery.inputmask

Here's an example jsfiddle: https://github.com/RobinHerbots/jquery.inputmask 这是一个jsfiddle示例: https : //github.com/RobinHerbots/jquery.inputmask

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

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