使用多个提交按钮在下拉更改或复选框检查时更新购物车
cart update on dropdown change or checkbox check with multiple submit buttons
我有一个购物车,现在有人可以更新产品数量(通过使用下拉列表)或完全删除产品(通过使用复选框),然后必须单击 "Update Cart"要更新的购物车。
为了方便起见,我尝试使用 "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 始终是 "quantity_select" 而 select 框的 ID 是“1”或“2”或“3”等
使用属性值 select 或定位所有 select 元素:
$('[name="quantity[]"]').change(function(){
$(this).closest('form').trigger('submit');
});
我有一个购物车,现在有人可以更新产品数量(通过使用下拉列表)或完全删除产品(通过使用复选框),然后必须单击 "Update Cart"要更新的购物车。
为了方便起见,我尝试使用 "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 始终是 "quantity_select" 而 select 框的 ID 是“1”或“2”或“3”等
使用属性值 select 或定位所有 select 元素:
$('[name="quantity[]"]').change(function(){
$(this).closest('form').trigger('submit');
});