cursor is jumping when pressing the arrow keys

前端 未结 5 970
日久生厌
日久生厌 2021-01-07 14:26

I have a textbox, where a forbidden character cant be typed. #.

This works, however, when the textbox is filled in with data, and I put the focus on the middle of th

5条回答
  •  情深已故
    2021-01-07 14:56

    You can prevent to execute your code if the user presses a right or a left arrow. To do this you just need to add this condition:

    if(e.which != 37 && e.which != 39){  
    

    You can find the key codes here.

    Your full code would be:

    $('[id$=txtClient]').keyup(function () {
        if(e.which != 37 && e.which != 39){  
            EnableClientValidateButton();
            ChangeColorClient("0"); 
            var $el = $('[id$=txtClient]'); 
            var text = $el.val(); 
            text = text.split("#").join("");
            $el.val(text);//set it back on the element
        }
    });
    

    LIVING EXAMPLE

提交回复
热议问题