-1

重複の可能性:
jQueryを使用してテキストボックス以外のバックスペースを無効にする方法

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

3 に答える 3

2

そのような jQuery オブジェクトを比較することはできません。必要なキー イベントは 1 つだけで、バックスペースはキー 9ではありません。

$(document).on('keydown', function(e) {
    if(e.keyCode === 8 && !$('#myTextBox').is(':focus')) {
        e.preventDefault();
    }
});
于 2012-12-24T04:56:25.117 に答える
0

event.target要素を取得するために使用するのはどうですか

function processKeyEvents(event) {
    // Backspace
    if (event.keyCode == 8) {
        // myTextBox is id of the valid textbox
        if (!$(event.target).is("#myTextBox")) {
            event.preventDefault();
        }
    }
} 
于 2012-12-24T05:07:42.357 に答える
0
$(document).keydown(function(e) {
    var elid = $(document.activeElement).hasClass('textInput');
    if (e.keyCode === 8 && !elid) {
        return false;
    };
});
于 2012-12-24T05:15:33.480 に答える