3

I am using jQuery. How to enable or disable textbox inside a table when checkbox checked. This is my edit.cshtml.

   <table id="scDetails" class="dataTable">
            <thead>
                <tr>
                    <th>RItem</th>
                    <th>IChecked</th>
                    <th>Notes</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var fback in Model.Fbacks)
                {
                    <tr>
                        <td @Html.DisplayFor(m => fback.RItem)</td>
                        <td>@Html.CheckBoxFor(m => fback.IChecked)</td>
                        <td>@Html.TextBoxFor(m => fback.Notes)</td>
                    </tr>
                }
            </tbody>
        </table>

What i have tried is:

 $('td :checkbox').change(function () {
            var parentTx = $(this).closest('tr').find(input[type = "text"]);
            if (this.checked) {
                parentTx.attr("disabled",false);
            } else {
                parentTx.attr("disabled", true);
            }
        });

Where am doing wrong? I dont want to use any classes on the controls to achieve this.

1 Answer 1

8

You missed the quotes, and you could simply your code with:

$('td input[type="checkbox"]').change(function () {
  $(this).closest('tr').find('input[type="text"]').prop('disabled', !this.checked);
}).change();
Sign up to request clarification or add additional context in comments.

6 Comments

@undefined Nice point, changed to use [type="..."] selector.
Thank you. it is working but by default when the page renders, the checkboxes which are not checked, those textboxes should be disabled. How can i get this working. Please.
@user1282609 trigger the change event after binding, check my update.
@user1282609 Did you refresh your page?
@user1282609 .val('') will do that for you.
|

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.