【问题标题】:javascript / ajax with one click set multiple variables by ids as an arrayjavascript / ajax 一键设置多个变量作为数组
【发布时间】: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,因为其他的,如 QuantityPrice 也由相同的 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-elements-by-id

get-multiple-id-at-once-at-set-a-function

display-results-of-event-on-multiple-id

感谢您阅读这里,哈哈 非常感谢各种帮助。

【问题讨论】:

    标签: javascript php jquery ajax


    【解决方案1】:
    $(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);
    });
    

    “但是我的脚本不起作用,上面写着:未捕获的 ReferenceError: save_id is not defined”

    嗯...错误消息告诉您发生了什么。您在 $.each 中声明 var。您在 $.each 之外控制台记录 save_id。 当然这不起作用(函数范围。)

    这可能有效,具体取决于您要对数组执行的操作。

    $(document).on('click', '#saveBtn, #view-table', function() {
        let id_array = [];
        $('#My_OrderCart_tbody tbody tr').each(function() {
            // who uses var anymore ?
            let save_id = $(this).find("dl[id^=dl]").data("place_id");
            // push to previously defined array
            id_array.push(save_id);
        });
        console.log(id_array[0]); // console log first id in array
    });
    

    如果您需要 id 的索引或元素,请将数据存储在对象中。

    let id_array = [];
    $('#My_OrderCart_tbody tbody tr').each(function() {
        let obj = {
            save_id: $(this).find("dl[id^=dl]").data("place_id"),
            elem: $(this)
        }
        
        // push to previously defined object
        id_array.push(obj);
    });
        console.log(id_array[0].save_id); // console log first id in array
    

    【讨论】:

    • 感谢您的指出!没想到让它成为数组,目前无法访问服务器,准备好后会尽快运行测试。
    • LOL 我仍然收到09:35:51.286 undefined index.php:305:12 相同的console.log 无法获得任何东西,我将尝试删除 id^=dl,测试如果只找到“dl”会发生什么, let 正在工作!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多