当前位置: 代码迷 >> JavaScript >> onekeydown 为 Firefox 65.0 ubuntu 18.04 中的每次键盘按下返回“Process”字符串
  详细解决方案

onekeydown 为 Firefox 65.0 ubuntu 18.04 中的每次键盘按下返回“Process”字符串

热度:99   发布时间:2023-06-05 14:15:16.0

我正在使用 Firefox 65.0 进行 Web 开发。 我的项目代码包含密码输入。 一年多前,我使用 JS 代码开发了它,该代码使用onkeydownonkeyup函数检测按键。 它适用于所有浏览器,除了新的 Firefox 65.0 版

我的代码类似于:

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type=password id="button" autocomplete_="off"></input> <script> $("#button").keydown(function(event) { console.log("key: " + event.key); }); document.getElementById("button").onkeydown = function(event) { console.log("key: " + event.key); }; </script>

对于这两种情况, event.key值不是按下的按钮,而是作为字符串的"Process" 有人知道为什么我会得到这个结果吗? 我该如何解决并检测真正的按键值?

我的操作系统版本:是 ubuntu 18.04

编辑:添加调试屏幕截图

另外我在事件下找不到任何可以帮助我的属性

您可以在 onkeydown 处理程序中找到“a”键情况下的调试屏幕截图。

键代码有很多不同的事件属性,它们都会返回一些不同的东西。 尝试event.which ,如果这不起作用,

编辑以提供更多信息

我做了一些测试,原因似乎是你试图在 type="password" 的输入上记录按键。 删除 type="password" 后,我在 Firefox 中测试了您的代码片段,并且event.whichevent.key可以正常工作。 我想它是由 Firefox 故意混淆的,以避免脚本记录密码击键。 如果您需要密码的值,请在事件侦听器中使用event.target.value 此外,我建议使用 'keyup' 事件而不是 keydown,否则您会遇到在按住某个键时反复触发侦听器的问题。

使用密码属性

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="password" id="button" autocomplete_="off"></input> <script> // Either remove the password type on your button, or access the event.target.value property and slice out the last keypress document.querySelector("#button").addEventListener('keyup', (event) => { console.log(`Which: ${event.which}, Key: ${event.key}`); console.log(`Value: ${event.target.value}`); }); </script>

没有密码属性

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input id="button" autocomplete_="off"></input> <script> // Either remove the password type on your button, or access the event.target.value property and slice out the last keypress document.querySelector("#button").addEventListener('keyup', (event) => { console.log(`Which: ${event.which}, Key: ${event.key}`); //console.log(`Value: ${event.target.value}`); }); </script>

  相关解决方案