Tee F
Tee F

Reputation: 285

Disable input with cshtml

I am trying to disable an input box based on if SuspendActivityAtNight is true or false. SuspendActivityAtNight is obtained from a checkbox. The problem is the input is enabled/disabled correctly to begin with. But when I click/unclick the checkbox, the input doesn't toggle for enabled/disabled state

<div class="col-md-6 col-sm-12 form-group">
    <label asp-for="Item1.SuspendActivity" class="col-md-8 control-label"></label>
    <div class="col-md-4">
        <input asp-for="Item1.SuspendActivity" type="time" class="form-control" disabled="@(!Model.Item1.SuspendActivityAtNight)" />
        <span asp-validation-for="Item1.SuspendActivity" class="text-danger"></span>
    </div>
</div>

Upvotes: 0

Views: 1103

Answers (1)

sam
sam

Reputation: 1985

Please add this change function to your razor file's scripts section (or create one if one is not there). Also replace chkBoxId and textboxId with your respective real html control ids.

$(document).ready(function() {

    $('#chkBoxId').change(function() {
        if(this.checked) {
           $("#textboxId").removeAttr("disabled");
        }
        else {
            $("#textboxId").attr("disabled", "disabled");
        }
    });
});

Upvotes: 0

Related Questions