user6063812 user6063812 - 2 months ago 9
Javascript Question

MVC Javascript Toggle TextInputFor Readonly based on checkbox value syntax?

Tried a few different ways but can't get it right and now as you can see it's all mixed up! This JavaScript business is not the same as it was 15 years ago! Thank you.



@section scripts
{
<script>
$(function () {
$("#cut").click(function () {
if (m.Fixture.IsCut.checked = true)
{
m.Fixture.BackOdds.readOnly == false;
}
else
{
m.Fixture.BackOdds.readOnly == true;
}
});
});
</script>
}

<div class="form-group">
@Html.LabelFor(m => m.Fixture.BackOdds)
@Html.TextBoxFor(m => m.Fixture.BackOdds, new { @id = "BackOdds", @class = "form-control", @readonly = "readonly" })
</div>
<div class="checkbox">
<label>
@Html.CheckBoxFor(m => m.Fixture.IsCut, new { @id = "cut", @onchange = "AutoCalculateMandateOnChange(this)"}) Odds Cut?
</label>
</div>




Answer

You need to refer to your element (m.Fixture.IsCut and m.Fixture.BackOdds are not elements in your DOM)

$("#cut").click(function () {
    $('#BackOdds').prop('readonly', !$(this).is(':checked'));
});