keypress在Mozila Firefox中不起作用 [英] keypress is not working in Mozila Firefox

查看:85
本文介绍了keypress在Mozila Firefox中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have got a task to restrict textbox from alphabetic values. Only floating point values are possible. After the decimal we have to write out two digits. I did this but it doesn't work in Mozilla Firefox. How can I solve this problem?

我的脚本是

$(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" />

这篇关于keypress在Mozila Firefox中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆