只有在输入中使用点(不是逗号)浮点值 - jQuery [英] Only float value with dot (not comma) in input - jQuery
问题描述
我想在输入中始终只使用点(不是逗号)浮动值。
I would like have always only float value with dot (not comma) in my inputs.
<input type="text" class="dot"> <br />
<input type="text" class="dot"> <br />
<input type="text" class="dot"> <br />
$('.dot').keydown(function(){
$(this).val($(this).val().toString().replace(/\,/g, '.'));
})
此替换逗号为点,但这不应该有> 1点和其他...这应该只接受[0-9]和。
this replace comma to dot, but this should not be have > 1 dot and others... This should be accept only [0-9] and .
如果我输入的值不同于其他[0-9]和。那么这个值应该被移除 - ''。
if i type a different value than other [0-9] and . then this value should be remove - ''.
我该怎么做?
推荐答案
Hiya 演示 http://jsfiddle.net/9Ry9t/ 或 http://jsfiddle.net/ZtkBW/2/ 或 http://jsfiddle.net/HJnLD/
Hiya demo http://jsfiddle.net/9Ry9t/ or http://jsfiddle.net/ZtkBW/2/ or http://jsfiddle.net/HJnLD/
与您的解决方案有所不同,但如果您希望使用此功能,则不会允许任何字符
键入文本框。
Bit different from your solution but if you keen to use this, it will not allow any characters
to type in the text box.
该解决方案对数字进行完全验证,对于浮点数不进行(点)
The solution does full validation for numbers and for float it will not take (dot)
代码示例
$('.number').keypress(function(event) {
if(event.which < 46
|| event.which > 59) {
event.preventDefault();
} // prevent if not number/dot
if(event.which == 46
&& $(this).val().indexOf('.') != -1) {
event.preventDefault();
} // prevent if already dot
});
这篇关于只有在输入中使用点(不是逗号)浮点值 - jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!