How can I disabling backspace key press on all browsers?

后端 未结 9 1124
一向
一向 2020-12-15 10:01

I\'m trying to disable the backspace button on an order page in all cases except when a textarea or text input is an active element to prevent users from accidentally backin

9条回答
  •  無奈伤痛
    2020-12-15 10:38

    Most examples seem to be for the JQuery framework - Here an example for ExtJS

    (I've been getting a lot of downvotes for this recently as the question now has JQuery tag on it, which it didn't previously. I can remove the answer if you like as isn't for JQuery but it's proven to help others not using that framework).

    To use this add this code block to your code base, I recommend adding it inside the applications init function().

        /**
         * This disables the backspace key in all browsers by listening for it on the keydown press and completely
         * preventing any actions if it is not which the event fired from is one of the extjs nodes that it should affect
         */
        Ext.EventManager.on(window, 'keydown', function(e, t) {     
           var nodeName = e.target.nodeName.toLowerCase();
            if (e.getKey() == e.BACKSPACE) {
                if ((nodeName === 'input' && e.target.type === 'text') ||
                    nodeName === 'textarea') {
                    // do nothing
                } else {
                    e.preventDefault();
                }
            }
        });
    

提交回复
热议问题