我有一个购物车,现在有人可以更新产品的数量(通过使用下拉列表)或完全删除产品(使用复选框),然后必须点击"更新购物车&#34 ;为了更新购物车。
为了方便起见,我尝试使用" update_cart"自动提交表单。提交按钮。请注意,表格中有第二个按钮,用于" checkout_cart"但只需手动点击一个。
<form action="cart.php" method="post">
<table class="table">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<tr>
<td>Item 1</td>
<td>
<select name="quantity[]" id="quantity_select">
<option value="1" selected >1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><input type="checkbox" name="delete" value="1" id="1"></td>
</tr>
<tr>
<td>Item 2</td>
<td>
<select name="quantity[]" id="quantity_select">
<option value="1" selected >1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><input type="checkbox" name="delete" value="2" id="2"></td>
</tr>
</tbody>
</table>
<input class="btn btn-primary" name="update_cart" type="submit" id="update_cart" value="Update Cart"></div>
<input class="btn btn-primary" name="checkout_cart" type="submit" id="checkout_cart" value="Checkout Cart"></div>
</form>
我尝试过类似的事情:
$('#1').change(
function(){
$(this).closest('form').trigger('submit');
});
和
$('#quantity_select').change(
function(){
$(this).closest('form').trigger('submit');
});
但那并没有奏效。问题是数量下拉ID总是&#34; quantity_select&#34;并且选择框的ID是&#34; 1&#34;或&#34; 2&#34;或&#34; 3&#34;等
答案 0 :(得分:1)
使用属性值选择器来定位所有选择元素:
$('[name="quantity[]"]').change(function(){
$(this).closest('form').trigger('submit');
});