【发布时间】:2022-01-20 06:01:27
【问题描述】:
对于各种建议和帮助,不胜感激。
我正在尝试构建一个功能,让购物车中的选定产品
移动到另一张桌子,以便我可以根据客户的要求重新订购它们。
我最大的问题是当我无法将它们转换为数组时,我不知道如何处理这种情况,omg
当产品放入购物车时会是这样的:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tbody class="table-border" id="My_OrderCart_tbody">
<tr>
<div class="row-fluid gx-2 px-2">
<dl data-place_id="dl7557">
<dd class="px-1">J. De Telmont Grand Reserve Brut NV Champagne (15Litre)</dd>
</dl>
</div>
<div class="px-2"><input class="input form-control" type="number" id="Quan7557" value="1" data-id="7557">
</div>
<div class="mx-auto"><span>$</span>
<input type="text" class="input form-control" id="Price7557" data-id="7557" value="10500">
</div>
<div class="row max-auto px-3">
<input type="button" class="btn btn-sm Remove" id="7557" value="Remove">
</div>
</tr>
<tr>
<span>SubTotoal :</span><span>$</span>
<input type="text" class="input form-control" id="Sub_7557" value="10500.00" disabled="">
</tr>
<tr>
<div class="row-fluid gx-2 px-2">
<dl data-place_id="dl7556">
<dd class="px-1">Taittinger Reserve Vintage Brut Champagne</dd>
</dl>
</div>
<div class="px-2"><input class="input form-control" type="number" id="new_Quan7556" value="1" data-id="7556">
</div>
<div class="mb-2 mx-auto">
<span>$</span><input type="text" class="input form-control" id="Price7556" data-id="7556" value="486">
</div>
<div class="row"><input type="button" class="btn Remove" id="7556" value="Remove">
</div>
</tr>
<tr>
<span>SubTotoal :</span><span>$</span>
<input type="text" class="input form-control fst-italic mbi-sub-text sub_total" id="new_Sub_a_7556" value="486.00" disabled="">
</tr>
<tr>
<div class="align-center">
<span class="text">Grand Total:</span>
<input type="text" id="gTotal1" value="10986" hidden="">
<span class="text" id="Grand_Total1" align="right">$ 10,986.00 </span>
</div>
</tr>
正如代码所示,产品都有不同的数据 ID,例如:
<dl class="list-group list-unstyled" data-place_id="dl7557">
<dl class="list-group list-unstyled" data-place_id="dl7556">
所以我试图获取数据 ID,因为其他的,如 Quantity、Price 也由相同的 ID 控制,但我使用了前缀:
<script>
$(document).on('click', '#saveBtn, #view-table', function() {
$('#My_OrderCart_tbody tbody tr').each(function() {
var save_id = $(this).find("dl[id^=dl]").data("place_id");
});
console.log(save_id);
});
</script>
但我的脚本不起作用,上面写着:Uncaught ReferenceError: save_id is not defined
另一方面,我知道我可以将整个 tbody 移动为以下代码:
<script>
$(document).on('click', '#saveBtn, #view-cf_table', function() {
var send_Table = document.getElementById('My_OrderCart_tbody');
var rec_Table = document.getElementById('locked_Confirm_Table');
rec_Table.innerHTML = send_Table.innerHTML;
});
</script>
但place-table 不应该与 Cart 完全相同, 还有另一件事是这些输入仍然保持输入,但理想情况下让它变为禁用将是表格中的最佳方式,
或者让我们获取文本并将它们放入其他元素中。
这也是添加到购物车时的 ajax 帖子:
<script>
$(document).ready(function(data){
$('.add_to_cart').click(function(){
var product_id = $(this).attr("id");
var product_name = $('#name'+product_id).val();
var product_price = $('#price'+product_id).val();
var product_quantity = $('#quantity'+product_id).val();
var action = "add";
if(product_quantity > 0)
{
$.ajax({
url:"action.php",
method:"POST",
dataType:"json",
data:{
product_id:product_id,
product_name:product_name,
product_price:product_price,
product_quantity:product_quantity,
action:action
},
success:function(data)
{
$('#order_table').html(data.order_table);
$('.badge').text(data.cart_item);
alert("Product has been Added into Cart");
}
});
}
else
{
alert("Please Enter Number of Quantity")
}
});
</script>
对于 PHP 站点,也使用 $_SESSION 和 $_POST 来处理值 如 pass-javascript-variable-to-php-and-then-to-cart-and-order-line-items-in-woo-com
如果有可能获得每个 dl 的 ID,那么 IMPO 很可能可以通过原始代码 $('prefix' + ids).val(); or .text(); 解决
甚至以为我找到了一些答案,例如将 ids 更改为 class,但我仍然需要 ids 来控制计算部分...
get-multiple-id-at-once-at-set-a-function
display-results-of-event-on-multiple-id
感谢您阅读这里,哈哈 非常感谢各种帮助。
【问题讨论】:
标签: javascript php jquery ajax