开发者

"freezing" an input box

开发者 https://www.devze.com 2023-03-23 16:24 出处:网络
I have an <input type=\"text\" /> element. At some point, I don\'t want it to accept text anymore. For some reason, changing the type to type=\"button\" for example is not allowed.

I have an <input type="text" /> element. At some point, I don't want it to accept text anymore. For some reason, changing the type to type="button" for example is not allowed.

How can I make my <input> eleme开发者_运维百科nt not accept text anymore?


Make it read-only:

$('#myInput').attr('readonly', 'readonly');


Just disable the control with:

<input type="text" disabled="disabled" />

But be warned, it is still possible for a hacker to manipulate submitted form data. You should ensure that appropriate server-side validation is applied.

If using jQuery you can do the following:

HTML:
<input id="my-text-box" type="text" />

JS - jQuery 1.5-:
$('#my-text-box').attr('disabled', 'disabled');
JS - jQuery 1.6+:
$('#my-text-box').prop('disabled', true);


Add the READONLY attribute (property in jQuery).


You need to modify the readonly attribute.


With JavaScript, you can do document.getElementById("textbox").disabled = "disabled";, which will make it read only.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号