I have an MVC app where I have a class of bools. There is a textarea next to each switch to enter comments if the item is indicated as rejected (returns false). I need to have the textarea disabled if the the swith is set to Accepted (returns true). I have searched ad nauseum and can't find a suitable solution. I know that there is an on-change event handler built in to bootstrap-switch but I am struggling to avoid an indivisual line for each bool field rather than using (this).
Here is an example of the code:
<div class="form-group">@Html.LabelFor(model => model.TaxTranscript, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-1">
<div class="checkbox">@Html.EditorFor(model => model.TaxTranscript) @Html.ValidationMessageFor(model => model.TaxTranscript, "", new {@class = "text-danger"})</div>
</div>
<div class="comments">@Html.Label("Comments", htmlAttributes: new { @class = "control-label col-md-1" })
<div class="col-md-4">@Html.EditorFor(model => model.TranscriptComments, new { htmlAttributes = new { @class = "form-control", @rows = 4 } }) @Html.ValidationMessageFor(model => model.TranscriptComments, "", new { @class = "text-danger" })</div>
</div>
</div>
<div class="form-group">@Html.LabelFor(model => model.AIR, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-1">
<div class="checkbox">@Html.EditorFor(model => model.AIR) @Html.ValidationMessageFor(model => model.AIR, "", new { @class = "text-danger" })</div>
</div>@Html.Label("Comments", htmlAttributes: new { @class = "control-label col-md-1" })
<div class="col-md-4 comments">@Html.EditorFor(model => model.AIRComments, new { htmlAttributes = new { @class = "form-control", @rows = 4 } }) @Html.ValidationMessageFor(model => model.AIRComments, "", new { @class = "text-danger" })</div>
</div>
And here is the script:
<script src="~/Scripts/mvcfoolproof.unobtrusive.min.js"></script>
<script>
$(function() {
$('input[type="checkbox"]').bootstrapSwitch('state', false, false);
$('input[type="checkbox"]').bootstrapSwitch('size', 'mini');
$('input[type="checkbox"]').bootstrapSwitch('onColor', 'success');
$('input[type="checkbox"]').bootstrapSwitch('onText', 'Accepted');
$('input[type="checkbox"]').bootstrapSwitch('offColor', 'danger');
$('input[type="checkbox"]').bootstrapSwitch('offText', 'Rejected');
$('input[name="TaxTranscript"]').on('switchChange.bootstrapSwitch', function(event, state) {
$(this).toggle('#TranscriptComments textarea');
});
});
</script>
I am using FoolProof requiring the textarea field be filled if the value is false.
Without seeing the rendered HTML, it is a little difficult to help. I have recreated your code in this demo bootply. Here's the code I used to init your checkboxes and handle the textarea toggling:
For this HTML:
Check out the linked bootply to see it work.
Note: For your code, making this change may possibly make it work:
The function
$.toggle()
doesn't take a selector argument. See the docs here.