-4

TEXTフィールド以外でBACKSPACEボタンを無効にしたい。

私は次のコードを使用していますが、テキストフィールドを含むバックスペース機能を妨げています..バックスペースはテキストフィールドでのみ機能するはずです.

これを助けてください...

$(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();
        }
    }
} 
4

2 に答える 2

1

keydown イベントを確認し、backspace押された場合は、テキストエリアがフォーカスされていない場合はデフォルトを防止します。 http://jsfiddle.net/Q9Meh/2/

$('body').keydown(function(event) {
    if (event.which == 8 && !$('textarea:focus').length) {
     event.preventDefault();
   }
});​
于 2012-12-23T15:19:08.027 に答える
0

これはどう?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();
        }
    }
}
于 2012-12-23T15:16:42.497 に答える