How to prevent user from typing in text field without disabling the field?

前端 未结 11 1690
不思量自难忘°
不思量自难忘° 2021-01-31 06:58

I tried:

$(\'input\').keyup(function() {

   $(this).attr(\'val\', \'\');

});

but it removes the entered text slightly after a letter is enter

11条回答
  •  天涯浪人
    2021-01-31 07:30

    If you want to prevent the user from adding anything, but provide them with the ability to erase characters:

    Setting the maxlength attribute of an input to "0" makes it so that the user is unable to add content, but still erase content as they wish.


    But If you want it to be truly constant and unchangeable:

    Setting the onkeydown attribute to return false makes the input ignore user keypresses on it, thus preventing them from changing or affecting the value.

提交回复
热议问题