如何使用jQuery禁用除文本框之外的退格
我想禁用BACKSPACE按钮,除非它在TEXT字段中。
我正在使用以下代码,但它阻止退格function包括文本字段.. BACKSPACE应仅适用于TEXT字段..
请帮忙解决这个问题……
$(document).on("keydown", processKeyEvents); $(document).on("keypress", processKeyEvents); function processKeyEvents(event) { // Backspace if (event.keyCode == 9) { // myTextBox is id of the valid textbox if ($("*:focus") != $("#myTextBox")) { event.preventDefault(); } } }
如果textarea没有聚焦,请检查keydown事件以及是否按下backspace
会阻止默认。 http://jsfiddle.net/Q9Meh/2/
$('body').keydown(function(event) { if (event.which == 8 && !$('textarea:focus').length) { event.preventDefault(); } });
这个怎么样? myTextBox
是退格应该起作用的文本框的id。
$(document).on("keydown", processKeyEvents); $(document).on("keypress", processKeyEvents); function processKeyEvents(event) { // Backspace if (event.keyCode == 8) { // myTextBox is id of the valid textbox if ($("*:focus") != $("#myTextBox")) { event.preventDefault(); } } }