Recently I had need for some visible input controls that were read only but where their values could still be posted with the form. Since I am working in an ASP.NET MVC project and not submitting this particular form asynchronously I wanted the model binding to bind the values of these fields. Typically there are two ways to accomplish having an input control that can not be edited.
The first of two ways is to use the disabled attribute. When disabled="disabled" an input element is visible but unable to be edited. Some browsers style these controls with a grey color to denote their inability to be edited, and some do not. Disabled controls do not post their values back when the form is submitted.
//Apply the disabled attribute
$('#myTextBox').attr("disabled", "disabled");
//Remove the disabled attribute
$('#myTextBox').removeAttr("disabled");
The second approach is to use the "readonly" attribute. I found that browsers do not style a read only control with the same grey color as some do with disabled, so I also applied a css class to the control. The value of a control with a "readonly" attribute set to true will still post values back.
//Apply the readonly attribute and greyBackground css class
$('#myTextBox').attr("readonly", true)
.addClass("greyBackground");
//Remove the readonly attribute and greyBackground css class
$('#myTextBox').removeAttr("readonly")
.removeClass("greyBackground");