简体   繁体   English

java脚本退格和删除在IE中工作,而不是在FireFox中

[英]java script backspace and delete working in IE,not in FireFox

I use the following function for decimal validation it was work fine in IE and Chrome not in FF.Backspace and delete key working in IE and Chrome.Not in FireFox 我使用以下函数进行十进制验证,它在IE中工作正常,Chrome不在FF.Backspace和删除键工作在IE和Chrome.Not在FireFox

    $('.decimalValidate').live('keypress', function (e) {
        var decimalid = $(this).attr("id");
        var decimalval = $('#' + decimalid).val();

        var decimalvalidate = ApplyDecimalFilter(decimalval, e);
        if (decimalvalidate == false) return false;
    });



    function ApplyDecimalFilter(id, event)
        {
            try {
                return NewDecimalFilter(id, event);
            } catch (e) {
                alert(e.message);
            }
        }

    function NewDecimalFilter(o, event) {
            if (event.which > 47 && event.which < 58) {
                return true;
            }
            if (event.which == 50 ||(event.which == 8 || event.which == 46) && o.indexOf('.') == -1)  {
                return true;
            }
            return false;
        }

this if condition not working in FireFox only.this is used to enter the only one dot symbol 此if条件仅在FireFox中不起作用。这用于输入唯一的点符号

if (event.which == 50 ||(event.which == 8 || event.which == 46) && o.indexOf('.') == -1)  {
                return true;
            }

In general, this type of validation needs to be done with care because text can be altered into an input via means other than the keyboard (pasting, text dragging and the Delete option in the context menu, for example). 通常,这种类型的验证需要谨慎进行,因为文本可以通过键盘以外的方式(例如粘贴,文本拖动和上下文菜单中的“删除”选项)更改为输入。 Limiting keyboard input will still need to be accompanied by proper validation when submitting the form. 在提交表单时,限制键盘输入仍需要伴随正确的验证。

Use keyCode for detecting the actual key pressed (usually in keydown or keyup rather than keypress ) and which to detect the character typed (only in the keypress event). 使用keyCode ,用于检测(通常在按压实际键keydownkeyup而不是keypress )和which检测输入(只在字符keypress事件)。 In general it's not a good idea to look at the keyCode property of keypress events but for the case of delete and backspace it's fine: not all browsers fire a keypress event for these keys but for those that do, the keyCode property is consistent. 一般来说,查看keypress事件的keyCode属性并不是一个好主意,但对于delete和backspace的情况,它很好:并非所有浏览器都为这些键触发了一个keypress事件,但对于那些键盘事件, keyCode属性是一致的。

In summary: change (event.which == 8 || event.which == 46) to (event.keyCode == 8 || event.keyCode == 46) and leave the rest the same. 总结:将(event.which == 8 || event.which == 46)更改为(event.keyCode == 8 || event.keyCode == 46) ,其余部分保持不变。

Here's the best reference for JavaScript key events I've seen: http://unixpapa.com/js/key.html 这是我见过的JavaScript关键事件的最佳参考: http//unixpapa.com/js/key.html

 $('#Name_Var').keypress(function (event) { event = event || window.event; var charCode = event.which || event.keyCode; var charStr = String.fromCharCode(charCode); // FireFox key Del - Supr - Up - Down - Left - Right if (event.key !== undefined && event.charCode === 0) { return; } //Only Num if (!/^([0-9])*$/.test(charStr)) { event.preventDefault(); } //Num and letters if (!/^[a-zA-Z0-9]+$/.test(charStr)) { event.preventDefault(); } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM