How to disable backspace if anything other than input field is focused on using jquery

后端 未结 9 837
南笙
南笙 2021-02-06 05:03

How do I disable backspace keystroke if anything other than 2 specific input fields are focused on using jquery?

here is my current code (NOW INCLUDING 2 TEXTBOXES):

相关标签:
9条回答
  • 2021-02-06 05:10

    I think a slight modification is needed to handle textareas:

    var elid = $(document.activeElement).is("input:focus, textarea:focus"); 
    
    0 讨论(0)
  • 2021-02-06 05:13

    This will disable backspace on your site without having to add specific classes to input boxes. To disable backspace except when using input boxes use .is("input:focus") if you want to disable backspace except when using textarea boxes use .is("input:focus, textarea:focus")

    $(document).keypress(function(e){ 
        var elid = $(document.activeElement).is("input:focus"); 
        if(e.keyCode === 8 && !elid){ 
           return false; 
        }; 
    });
    
    0 讨论(0)
  • 2021-02-06 05:20

    I too have worked hard on the same. Finally, I found the answer and for everybody's convenience I've placed the solution on http://blog.totusinfo.com/jquery-disable-backspace-for-document-history-except-inputs-and-textarea/

    0 讨论(0)
  • 2021-02-06 05:25

    I think this would do the trick:

    $(document).keydown(function(e) {
        var elid = $(document.activeElement).hasClass('textInput');
        if (e.keyCode === 8 && !elid) {
            return false;
        };
    });
    

    assuming that the textboxes has the class 'textInput'.

    Here is a working example

    0 讨论(0)
  • 2021-02-06 05:25

    I made a slight change to the accepted answer, which may be of use to someone:

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

    With this method, the user can still navigate using Backspace by holding CTRL, but also it takes into account textarea as well as any input.

    Of course, the alternative is to use something like this instead, which doesn't allow the user to leave the page if they've inputted anything.

    0 讨论(0)
  • 2021-02-06 05:27

    The solution of Hudson-Peralta + QF_Developer is great, but it has one flaw:
    If your focus is on a radio button or checkbox the backspace button will still throw you back out of the page. Here is a modification to avoid this gap:

    $(document).keydown(function(e){ 
      var elid = $(document.activeElement).is('input[type="text"]:focus, textarea:focus'); 
        if(e.keyCode === 8 && !elid){ 
          return false; 
        }; 
    });
    

    EDIT 20130204:
    keypress() replaced by keydown()!
    The code above now works correctly.

    EDIT 20151208:
    As mentioned in the comment of @outofmind there are more input types that could throw you back when backspace is pressed. Please add to the comma separated selector list of the is() method any type of input fields that allow direct character input and that are really used in your HTML code, like input[type="password"]:focus, input[type="number"]:focus or input[type="email"]:focus.

    0 讨论(0)
提交回复
热议问题