简体   繁体   中英

How to allow only backspace and movements in a text area

I'm looking for a jquery function that only allows a movements with arrow keys and backspace in a textarea, but the movements with the arrow keys don't work correctly.

 function moveArrow(e){
    if(e.which >= 37 && e.which <= 40){     
        return true;    
    }
    return false;
 }

$(document).ready(function() {
  $('#installId').keypress(function (e) 
    { return deleteItem(e,false) });

  $('#installId').keydown(function (e) 
    { return deleteItem(e,false) }); 

  $('#installId').keypress(function (e) 
        { return moveArrow(e) });

  $('#installId').keydown(function (e) 
        { return moveArrow(e) }); 
});

you have to preventDefault on other key press

$('#installId').keypress(function (e){
        if(e.which < 37 || e.which > 40)
              e.preventDefault();
    });

Working example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM