Javascript 空格键 keyCode(32) 在 Mozilla Firefox 上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14029708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:29:15  来源:igfitidea点击:

Space bar keyCode(32) not working on Mozilla Firefox

javascriptfirefoxspacekeycodeonkeypress

提问by Fi3n1k

Enter keyCode(13)works fine on all browsers.

EnterkeyCode(13)在所有浏览器上都可以正常工作。

Space bar keyCode(32)I tested on Chrome works fine but not responding on Firefox. I used the following code:

keyCode(32)我在 Chrome 上测试的空格键工作正常,但在 Firefox 上没有响应。我使用了以下代码:

<script type="text/javascript" >
    function enterPressed(evn) {
        var e_id = $('e_id').value;
        var e_fname = $('e_fname').value;
        var e_role = $('e_role').value;

        if (window.event && window.event.keyCode == 13) {
            Monitor.Order.assign(e_id, e_fname, e_role);
        } else if (evn && evn.keyCode == 13) {
            Monitor.Order.assign(e_id, e_fname, e_role);
        } else if (evn && evn.keyCode == 32) {
            Monitor.Order.updateStatus('COOKED');
        }                       
    }
    document.onkeypress = enterPressed;     
</script>

Why is this not working in Firefox when it works in Chrome?

为什么它在 Chrome 中工作时在 Firefox 中不起作用?

回答by Boris Zbarsky

Space is a printable character, so the keypress event will have the charCodeset to the character that it corresponds to and the keyCodewon't be set on the keypress event in Firefox.

Space 是一个可打印的字符,因此 keypress 事件将charCode设置为其对应的字符,并且keyCode不会在 Firefox 中的 keypress 事件上设置。

In general, you want to use charCodefor printable things in keypress, keyCodein keyup/keydown.

通常,您希望charCode在 keypress 中使用可打印的东西,keyCode在 keyup/keydown 中。

回答by Nipun Jain

try this code it will work fine ....

试试这个代码它会工作得很好....

function enterPressed(evn) {
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x 

        var e_id = $('e_id').value;
        var e_fname = $('e_fname').value;
        var e_role = $('e_role').value;

        if (evn.which == 13) {
            Monitor.Order.assign(e_id, e_fname, e_role);
        } else if (evn.which == 13) {
            Monitor.Order.assign(e_id, e_fname, e_role);
        } else if (evn.which == 32) {
            Monitor.Order.updateStatus('COOKED');
        }                
    }

     else{
            var e_id = $('e_id').value;
            var e_fname =$('e_fname').value;
            var e_role = $('e_role').value;

            if (window.event && window.event.keyCode == 13) {
                Monitor.Order.assign(e_id, e_fname, e_role);
            } else if (evn && evn.keyCode == 13) {
               Monitor.Order.assign(e_id, e_fname, e_role);
            } else if (evn && evn.keyCode == 32) {
                Monitor.Order.updateStatus('COOKED');
            }  

        }
    }
    document.onkeypress = enterPressed;