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.
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.
Share Improve this question edited Sep 26, 2012 at 2:19 Eli 14.8k5 gold badges61 silver badges77 bronze badges asked Sep 26, 2012 at 2:08 user1282609user1282609 5752 gold badges14 silver badges32 bronze badges1 Answer
Reset to default 8You 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();