如果使用jquery关注2个特定输入字段以外的其他内容,如何禁用退格键击?
这是我目前的代码(现在包括2个文本框):
$(document).keypress(function(e){
var elid = $(document.activeElement).attr('id');
if(e.keyCode === 8 && elid != 'textbox1' || elid != 'textbox2'){
return false;
};
});
这不起作用......任何想法?
答案 0 :(得分:12)
我认为这样可以解决问题:
$(document).keydown(function(e) {
var elid = $(document.activeElement).hasClass('textInput');
if (e.keyCode === 8 && !elid) {
return false;
};
});
假设文本框的类为“textInput”。
答案 1 :(得分:8)
这将禁用您网站上的退格,而无需在输入框中添加特定类。要禁用退格键,除非使用输入框时使用 .is(“input:focus”)如果要禁用退格键,除非使用textarea框时使用 .is(“input:focus,textarea:焦点“)强>
$(document).keypress(function(e){
var elid = $(document.activeElement).is("input:focus");
if(e.keyCode === 8 && !elid){
return false;
};
});
答案 2 :(得分:6)
Hudson-Peralta + QF_Developer的解决方案很棒,但它有一个缺陷:
如果你的焦点在一个单选按钮或复选框上,退格按钮仍然会让你退出页面。这是一个避免这种差距的修改:
$(document).keydown(function(e){
var elid = $(document.activeElement).is('input[type="text"]:focus, textarea:focus');
if(e.keyCode === 8 && !elid){
return false;
};
});
编辑20130204:
keypress()
替换为keydown()
!
上面的代码现在可以正常工作。
编辑20151208:
正如@outofmind的注释中所提到的,当按下退格键时,有更多的输入类型可以让你回头。请在is()方法的逗号分隔选择器列表中添加任何类型的输入字段,这些字段允许直接输入字符并在HTML代码中使用,例如input[type="password"]:focus
,input[type="number"]:focus
或{{1 }}
答案 3 :(得分:5)
我认为需要稍微修改来处理textareas:
var elid = $(document.activeElement).is("input:focus, textarea:focus");
答案 4 :(得分:4)
我对接受的答案稍作修改,这可能对某人有用:
$(document).keydown(function(e) {
var elid = $(document.activeElement).is("input, textarea") ;
if (e.keyCode === 8 && !elid) {
if(e.ctrlKey) {
window.history.back()
}
else {
alert("Navigating with Backspace has been disabled. Use CTRL + Backspace if you want to go back to the previous page (and lose any unsaved changes).");
return false;
}
}
});
使用此方法,用户仍然可以通过按住CTRL来使用Backspace进行导航,但它也会考虑textarea
以及任何input
。
当然,替代方法是使用类似this的内容,如果用户输入任何内容,则不允许用户离开页面。
答案 5 :(得分:2)
@Nick Craver,出于几个原因可耻的答案。回答这个问题或者至少光顾一下。
这是我最终用于表单的基于原型的解决方案,因为用户抱怨退格会将它们从表单中取出(这显然是违反直觉的事情,人们想知道为什么所有浏览器都使用退格键作为后退按钮)。
// event handlers must be attached after the DOM is completely loaded
Event.observe(window, 'load', function() {
// keypress won't fire for backspace so we observe 'keydown'
Event.observe(window, 'keydown', function(event){
// 8 == backspace
if( event.keyCode == 8) {
// with no field focused, the target will be HTMLBodyElement
if( event.target == document.body) {
// stop this event from propagating further which prevents
// the browser from doing the 'back' action
event.stop();
}
}
});
});
答案 6 :(得分:2)
Hudson-Peralta的回答对我有用,但由于我使用了许多keydown事件,我做了一个小修改:
$(document).keydown(function(e){
var elid = $(document.activeElement).is("input:focus");
if(e.keyCode === 8 && !elid){
e.preventDefault();
};
});
答案 7 :(得分:0)
我也在努力工作。最后,我找到了答案,为了方便每个人,我已将解决方案放在http://blog.totusinfo.com/jquery-disable-backspace-for-document-history-except-inputs-and-textarea/
上答案 8 :(得分:0)
我把答案修改了一下,结合每个人的好答案
1.使用选择器“input [type = text]:focus,textarea:focus”来维护这些元素的默认行为,正如JPsy所说,防止输入上的默认行为,如复选框
2.使用e.target代替使代码更多地依赖于JQuery,因为我不确定每个浏览器是否都支持document.activeElement
修改强>
3.添加“输入[type =密码]:焦点”,
$(document).keydown(function(e){ var code=e.keyCode; var is_to_stop= (!$(e.target).is("input[type=text]:focus, input[type=password]:focus, textarea:focus")) && (code==8); if(is_to_stop){ return false; } });