admin管理员组

文章数量:1201836

Is there a simple Javascript or JQuery way to make a textbox disabled WITHOUT using the attribute disabled?

We are using Handlebar.js and when a disabled field is found it skips the field when its serialized so we can't use the disabled attribute. Is there a way to make the textbox uneditable still? Perhaps with a focus or blur?

Is there a simple Javascript or JQuery way to make a textbox disabled WITHOUT using the attribute disabled?

We are using Handlebar.js and when a disabled field is found it skips the field when its serialized so we can't use the disabled attribute. Is there a way to make the textbox uneditable still? Perhaps with a focus or blur?

Share Improve this question asked Jul 18, 2014 at 8:33 cdubcdub 25.7k60 gold badges186 silver badges327 bronze badges 4
  • 3 Can you use Readonly? – Grim Commented Jul 18, 2014 at 8:35
  • I'll test readonly now. Thanks – cdub Commented Jul 18, 2014 at 8:38
  • Read more about stackoverflow.com/questions/2458595/disable-a-textbox-using-css – sajid Commented Jul 18, 2014 at 8:43
  • The link isn't what was asked – cdub Commented Jul 18, 2014 at 8:48
Add a comment  | 

3 Answers 3

Reset to default 16

Use readonly attribute instead :

$('#textBox').prop('readonly', true);

Use .attr() instead of .prop() if you're using jQuery < 1.9

directly add "readonly" attribute to the input element like this

Name: <input type="text" value="editing me is disabled" readonly>

It works like charm. No need of using jquery for that.

document.forms['formName']['inputName'].readOnly = true;

本文标签: javascriptDisable a text box without using disabledStack Overflow