当 jQuery 中输入复选框聚焦时,如何禁用退格键?
当输入复选框
在 jQuery 中聚焦时,如何禁用退格按钮?当聚焦于复选框
并按退格键 (keycode=8) 时,浏览器认为我正在尝试返回页面并使用退格键作为 history.go(-1)命令。
$("div#types input").focus(function(){
window.onkeydown = function(e){
return !(e.keyCode == 8);
};
How do I disable the backspace button when input checkboxes
are focused on in jQuery? When checkboxes
are focused on and I press backspace (keycode=8) the browser thinks I'm trying to go back a page and uses backspace as a history.go(-1)
command.
$("div#types input").focus(function(){
window.onkeydown = function(e){
return !(e.keyCode == 8);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这将始终阻止退格按钮向后导航:
现在要弄清楚如何仅在复选框获得焦点时才运行它...
编辑:
确定复选框是否具有焦点的问题是,它只有在它获得焦点时才获得焦点是在代码中选项卡或实际设置的。如果单击它,则会检查它,但实际上它不会聚焦(至少在 chrome/safari 中)。这一切都取决于您如何设置焦点。
编辑2:
要使您的复选框在单击时获得焦点,只需添加以下内容:
...所以将它们放在一起,这将使复选框焦点单击并在复选框获得焦点时停止退格按钮(全部在 $( document).ready函数当然):
Well this will stop the backspace button at all times from navigating back:
Now to figure out how to run this only when checkboxes are focused...
Edit:
The problem with figuring out if a checkbox has focus is that it only gets focus if it is tabbed to or actually set in code. If it is clicked it is checked but it actually does not focus (at least in chrome/safari). This all depends on how you set the focus.
Edit 2:
To make your checkbox have focus when it is clicked, just add this:
...so putting it all together, this would have the checkbox focus on click and stop the backspace button when a checkbox has focus (all inside $(document).ready function of course):