我目前有一個表格,一邊是複選框和產品名稱,另一邊是數量輸入字段。默認情況下,數量字段處於禁用狀態,但我希望僅在檢查其相應產品時啓用。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