[英]plus sign in html input type=number
Does anyone know if it's possible to add a plus sign in a html input type=number field?有谁知道是否可以在 html input type=number 字段中添加加号? I have a scenario whereby users need to enter a modifier value which is either +1 to +10 or -1 to -10.
我有一个场景,用户需要输入一个修饰符值,该值是 +1 到 +10 或 -1 到 -10。 I want to make it very explicit to the user that their inputted value increments the resulting value.
我想让用户非常明确地知道他们输入的值会增加结果值。 therefore it would be great if a + sign is prepended to a positive inputted value.
因此,如果在正输入值前加上 + 号,那就太好了。
I'm using this input in an Angular2 reactive form with bootstrap styling.我在带有引导样式的 Angular2 反应形式中使用此输入。 so an Angular2 directive, a boostrap style or even a jquery solution are welcome
所以欢迎使用 Angular2 指令、boostrap 风格甚至 jquery 解决方案
What I've seen so far is about adding currency symbols inside the form control or about adding + and minus buttons to increment and decrement the value.到目前为止,我所看到的是在表单控件中添加货币符号或添加 + 和减号按钮来增加和减少值。 But that's not what I want, so I doubt if this is possible at all.
但这不是我想要的,所以我怀疑这是否可能。
No it's not possible.不,这是不可能的。 The number field only accepts
-
, 0-9
and e
to be entered.数字字段只接受
-
、 0-9
和e
输入。 As a workaround you could place the +
in the HTML before the input, eg:作为一种解决方法,您可以在输入之前将
+
放在 HTML 中,例如:
+ <input type="number" />
Alternatively you could use a standard type="text"
input, but that would mean creating your own validation logic which would make the code much more complex.或者,您可以使用标准
type="text"
输入,但这意味着创建您自己的验证逻辑,这会使代码更加复杂。
I don't think it is possible to manipulate the input.我认为不可能操纵输入。 the default is a "-" for negative and no sign for positive numbers.
默认为负数的“-”,正数没有符号。
even if you checked a create UI frameworks like:即使您检查了创建 UI 框架,例如:
Jquery UI, Bootstrap, Angular Material ... Jquery UI、Bootstrap、Angular Material ...
I guess the only solution is to write your own custom code.我想唯一的解决方案是编写自己的自定义代码。
two scenarios are available:有两种方案可用:
1- an input with Text and the manipulating will be in javascript. 1-带有文本的输入和操作将在 javascript 中。
2- create a pipe in angular2 and give it to the element. 2-在 angular2 中创建一个管道并将其提供给元素。 which I think it is much easier than the first one.
我认为这比第一个容易得多。
I hope this will help on you guys!!我希望这对你们有帮助!!
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).on('keypress','#input',function (e) { var keyCode = e.which; if ($(this).val().trim().charAt(0) == "\+") { if ( keyCode == 43 ) { return false; } } else{ for (var i = 1; i < $(this).val().length+1; i++) { if(i!=0){ if ( keyCode == 43 ) { return false; } } } } }); $(document).on('input', '#input', function() { this.value = this.value.replace(/[^+{1}|^0-9]/g,''); }); </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.