我在输入时只有允许使用jquery库的浮点数时有痛苦的时间.我的代码无法阻止chacacter"." 当它成为第一个输入时,任何人都可以指导我解决这个问题吗?
$('.filterme').keypress(function(eve) {
if ( ( eve.which != 46 || $(this).val().indexOf('.') != -1 )
&& ( eve.which < 48 || eve.which > 57 )
|| ( $(this).val().indexOf('.') == 0)
)
{
eve.preventDefault();
}
});?
Run Code Online (Sandbox Code Playgroud)
bil*_*oah 23
我用它 - 用于键盘输入或复制和粘贴
$('input.float').on('input', function() {
this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<input type="text" class="float" />Run Code Online (Sandbox Code Playgroud)
说明:
我使用jQuery Caret插件过滤第一个位置输入.否则,一旦输入了点,检查它的位置已经很晚了.我尝试检查点,然后删除点,但它看起来不太好.
jQuery插入符插件:http: //examplet.buss.hk/js/jquery.caret.min.js
我做了什么:
http://jsfiddle.net/FCWrE/422/
试试吧 :)
$('.filterme').keypress(function(eve) {
if ((eve.which != 46 || $(this).val().indexOf('.') != -1) && (eve.which < 48 || eve.which > 57) || (eve.which == 46 && $(this).caret().start == 0)) {
eve.preventDefault();
}
// this part is when left part of number is deleted and leaves a . in the leftmost position. For example, 33.25, then 33 is deleted
$('.filterme').keyup(function(eve) {
if ($(this).val().indexOf('.') == 0) {
$(this).val($(this).val().substring(1));
}
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/caret/1.0.0/jquery.caret.min.js"></script>
<input type="text" class="filterme">Run Code Online (Sandbox Code Playgroud)