I'm using ASP.NET MVC 4 and jQuery to achieve this.
What I'm trying to do is to disable or enable a dropdownlist based on if a checkbox is checked or not.
I can get this working, however I can't seem to disable the dropdownlist on pageload. the statement is executed, but the element is not disabled.
What I think the problem is, is that the check is performed, BEFORE the dropdownlist (from the viewmodel) is populated, and the data somehow overrides?
Here's my HTML:
<p>
@Html.CheckBoxFor(model => model.RoomRequired, new { id= "room-required" })
<label>Room Required</label>
</p>
<p>
<label>Room Options</label>
@Html.DropDownListFor(model => model.OptionId, new SelectList(Model.RoomOptions, "Id", "Name"), "Select option", new { id="option-select })
</p>
and fixed Javascript:
$(document).ready(function () {
$('#room-required').change(function () {
$('#option-select').prop('disabled', $(this).is(':checked'));
});
$('#room-required').trigger('change');
});