javascript - 按键在 Mozilla Firefox 中不起作用

标签 javascript jquery html

我的任务是限制文本框使用字母值。只有浮点值是可能的。小数点后我们必须写出两位数字。我这样做了,但它在 Mozilla Firefox 中不起作用。我该如何解决这个问题?

我的脚本是

$(function () {
    $('#name').bind('paste', function () {
        var self = this;
        setTimeout(function () {
            if (!/^[a-zA-Z]+$/.test($(self).val())) $(self).val('');
        }, 0);
    });

    $('#salary').bind('paste', function () {
        var self = this;
        setTimeout(function () {
            if (!/^\d*(\.\d{1,2})+$/.test($(self).val())) $(self).val('');
        }, 0);
    });

    $('.decimal').keypress(function (e) {
        var character = String.fromCharCode(e.keyCode)
        var newValue = this.value + character;
        if (isNaN(newValue) || hasDecimalPlace(newValue, 3)) {
            e.preventDefault();
            return false;
        }
    });

    function hasDecimalPlace(value, x) {
        var pointIndex = value.indexOf('.');
        return  pointIndex >= 0 && pointIndex < value.length - x;
    }
     function isNumberKey(evt) { 
         var charCode = (evt.which) ? evt.which : event.keyCode 

         if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) 
             return false; 
         else { 
             var len = document.getElementById("txtChar").value.length; 
             var index = document.getElementById("txtChar").value.indexOf('.'); 

             if (index > 0 && charCode == 46) { 
                 return false; 
             } 
             if (index >0 || index==0) { 
                 var CharAfterdot = (len + 1) - index; 
                 if (CharAfterdot > 3) { 
                     return false; 
                 } 

}

         } 
         return true; 
      } 
});

html是

<b>Name</b>
<input type="text" id="name"  /><br/>
<b>Salary</b>
<input type="text" id="txtChar" onkeypress="return isNumberKey(event)"  name="txtChar" class="CsstxtChar" />

最佳答案

function isNumberKey(evt) { 
     var charCode = (evt.charCode) ? evt.which : event.keyCode

     if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) 
         return false; 
     else { 
         var len = document.getElementById("txtChar").value.length; 
         var index = document.getElementById("txtChar").value.indexOf('.'); 

         if (index > 0 && charCode == 46) { 
             return false; 
         } 
             if (index >0 || index==0) { 
                 var CharAfterdot = (len + 1) - index; 
                 if (CharAfterdot > 3) { 

                     return false; 
                 } 

}

         } 
         return true; 
      } 





<input type="text" id="txtChar" onkeypress="return isNumberKey(event)"  name="txtChar" class="CsstxtChar" />

关于javascript - 按键在 Mozilla Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15699654/

相关文章:

javascript - 避免克隆 HTMLTemplate 来检索 innerHTML?

javascript - 保存文件 Python - IOError 权限被拒绝

javascript - 在键入 bootstrap select jquery 时加载选择选项

javascript - 在 jquery 网格中隐藏页面导航器

html - 全 div 宽度的文本框和按钮

html - CSS 列,从上到下然后从左到右

javascript - 如何使用 javascript 更改基于另一个下拉列表的下拉列表值?

javascript - Dropkick - 选择问题数组

javascript - 选定的 ng-option 与其数据不匹配以及空选项问题

javascript - Div 内容在移动 View 中消失