我目前有一个表格,一边是复选框和产品名称,另一边是数量输入字段。默认情况下,数量字段处于禁用状态,但我希望仅在检查其相应产品时启用。jQuery - 复选框勾选启用它旁边的字段
我还是新来的jQuery,有点停留在此,所以这是一个最好的我能想出:输入元素
$(document).ready(function(){
//enable quantity field if product is selected
$("input[name='quantity']").prop('disabled', 'true');
$(".product").on('click', function(){
$next = $(this).next();
$next.prop('disable', 'false');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="products">
<thead>
<tr>
<th>Product Name</th>
<th width="150">Quantity</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type='checkbox' id='product' class='product' name='product'><label>Product</label></td>
<td><input type='text' placeholder='0' name='quantity' id='quantity'></td>
</tr>
<tr>
<td><input type='checkbox' id='product' class='product' name='product'><label>Product</label></td>
<td><input type='text' placeholder='0' name='quantity' id='quantity'></td>
</tr>
<tr>
<td><input type='checkbox' id='product' class='product' name='product'><label>Product</label></td>
<td><input type='text' placeholder='0' name='quantity' id='quantity'></td>
</tr>
</tbody>
</table>
我通常使用复选框伪类订阅的复选框事件的变化,如'$( ':复选框')有更好的运气。变化( function(){...})' – SciGuyMcQ