Prevent backspace from navigating back in AngularJS

后端 未结 4 1566
Happy的楠姐
Happy的楠姐 2021-01-12 12:03

I faced this issue in my AngularJS webapp.

When a user enters a page with a form to fill and he starts typing, if he presses the backspace key and the focus is not o

4条回答
  •  时光取名叫无心
    2021-01-12 12:54

    Add the below script in your controller

    var rx = /INPUT|SELECT|TEXTAREA/i;
    
    $document.on("keydown keypress", function(e){
        if( e.which == 8 ){ // 8 == backspace
            if(!rx.test(e.target.tagName) || e.target.disabled || e.target.readOnly ){
                e.preventDefault();
            }
        }
    });
    

    Or you can use Jquery

      $(function(){
          var regx = /INPUT|SELECT|TEXTAREA/i;
    
          $(document).bind("keydown keypress", function(e){
              if( e.which == 8 ){ // 8 == backspace
                  if(!regx.test(e.target.tagName) || e.target.disabled || e.target.readOnly ){
                      e.preventDefault();
                  }
              }
          });
      });
    

提交回复
热议问题