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.
精彩评论