Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 禁用输入键按入类型按钮_Javascript_Html - Fatal编程技术网

Javascript 禁用输入键按入类型按钮

Javascript 禁用输入键按入类型按钮,javascript,html,Javascript,Html,如何禁用“回车”按钮 我正在做一个游戏,你按下一个按钮,然后计数器计数你按下并显示文本,问题是当你按下回车键时,计数器会快速上升。。。这是游戏 那么,如何禁用Javascript中默认的按键功能?您可以尝试以下代码: $('#yourButtonId').keypress(function(event) { if (event.which == 13) { event.preventDefault(); } }); 希望这会有所帮助 document.getEl

如何禁用“回车”按钮

我正在做一个游戏,你按下一个按钮,然后计数器计数你按下并显示文本,问题是当你按下回车键时,计数器会快速上升。。。这是游戏

那么,如何禁用Javascript中默认的按键功能?

您可以尝试以下代码:

$('#yourButtonId').keypress(function(event) {
    if (event.which == 13) {
        event.preventDefault();
    }
});
希望这会有所帮助

document.getElementById("countButton").onkeydown = function(e){
if (e.which == 13) //13 is the keycode referring to enter.
    {
       e.preventDefault(); //this will prevent the intended purpose of the event. 
       return false; //return false on the event.
    }
}
这将阻止按enter键执行按钮

高级解决方案。只允许输入一次。用户必须松开enter按钮才能重置

var enterPressed = 0;
document.getElementById("countButton").onkeydown = function(e){
    if (e.which == 13)
        {
        if (!enterPressed)
        {
            enterPressed = 1;
            return true;
        }
        else
        {
            e.preventDefault();
            return false;
        }


    }
}

document.getElementById("countButton").onkeyup = function(e){
    if (e.keyCode == 13)
    {
        enterPressed = 0;

    }
}

通常我会提倡使用
addEventListener
,但是这是一个非常简单的网站,只有一个目的,内联事件在这里不是问题。

请提供一些代码。按键不会注册enter。使用keydown.OP似乎不使用jQuery,而是使用vanilla JS。