0

My submit.cshtml is:

<table id="scDetails" class="table">
    <thead>
        <tr>
            <th>Item</th>
            <th>IsChecked</th>
            <th>Comment</th>
        </tr>
    </thead>
    <tbody>
        @Html.EditorFor(m => m.Fbacks)
    </tbody>
</table>

EditorTemplate is for - @Html.EditorFor(m => m.Fbacks) (Fbacks.cshtml)

<tr>
  <td>@Html.HiddenFor(m => m.Item)
    @Html.DisplayFor(m => m.Item)
  </td>
  <td>@Html.CheckBoxFor(m => m.IsChecked)</td>
  <td>@Html.TextBoxFor(m => m.Comment)</td>
</tr>

What I need is:

When I check the checkbox, textbox should be enabled and when uncheck, textbox should disabled.

1
  • Which version of jQuery are you using? Commented Sep 25, 2012 at 7:37

3 Answers 3

2
$("input[type='checkbox']").on("click", function() {
    $(this).parent().next().find("textarea").prop("disabled", ! this.checked);
});
Sign up to request clarification or add additional context in comments.

1 Comment

This need to be written on submit.cshtml or Fbacks.cshtml page? Do I need to replace "textarea" with "textbox". Please.
1
$('input[type=checkbox]').on('click', function() {
    var isChecked = $(this).is(':checked');

    $(this).parent().next().find('input[type="text"]').attr('disabled', !isChecked);
}); 

// OR

$('input[type=checkbox]').on('click', function() {
        var isChecked = $(this).is(':checked');

        $(this).parent().parent().find('input[type="text"]').attr('disabled', !isChecked);
    }); 

1 Comment

Do i need to specify any Id on EditorTemplate? Fbacks.cshtml page? or Submit.cshtml page
1

You should add event to checkbox

$('#Fbacks').click (function ()
{
     var thisCheck = $(this);
     if (thischeck.is (':checked'))
     {
       $('#Comment').prop('disabled', true);
     }else{
       $('#Comment').prop('disabled', false);
     }
});

in this case @Html.EditorFor(m => m.Fbacks) id = 'Fbacks'

4 Comments

Do i need to specify any Id on EditorTemplate?
This need to be written on Submit.cshtml or? EditorTemplate page?
This can be written in any place, The main think that this code executed after document.ready(). On the bissuness logic i will be put it on EditorTemplate as it logic of editForm.
but the best place of js code is separate file, which should be included in master page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.