I need help creating a JavaScript function that will disable certain form elements (EditorsFor) when a checkbox is unchecked and enable them when it is checked. I have tried various codes from the internet but none seem to work.
Here is a snippet of my View code:
@{Html.RenderPartial("_PartialError");}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
(...)
<div class="form-group">
@Html.LabelFor(model => model.Czy_zuzywalny, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Czy_zuzywalny)
@Html.ValidationMessageFor(model => model.Czy_zuzywalny, "", new { @class = "text-danger" })
</div>
</div>
</div>
(...)
}
I have attempted to write a JavaScript function in a separate file, but it doesn't seem to be working as expected:
$(document).ready(function ()
{
$("#Czy_zuzywalny").click(function ()
{
if ($("#Ilosc_minimalna").attr("disabled") == "")
{
$("#Ilosc_minimalna").attr("disabled", "disabled");
$("#Ilosc_optymalna").attr("disabled", "disabled");
$("#Ilosc_minimalna").attr("value", "0");
$("#Ilosc_optymalna").attr("value", "0");
}
else
{
$("#Ilosc_minimalna").attr("disabled", "");
$("#Ilosc_optymalna").attr("disabled", "");
}
});
});
Unfortunately, the code does not work as intended. Can anyone offer guidance on how to solve this issue?