0

我有各种产品的链接。我正在使用 php 将数据库中的产品 ID 添加为每个链接的 id 值。

<a class="order_btn" id="<?php echo $row['product_id'];?>"><?php echo $row['product_name']; ?></a>

我正在使用 jquery cookie 插件(https://github.com/carhartl/jquery-cookie)来保存单击订单按钮的次数。我使用 id 属性中的产品 ID 作为已保存 cookie 的键。

$(".order_btn").click(function() {
  var productToAdd = $(this).attr("id");

  if($.cookie("Product-"+productToAdd) >= 1) { //If this is not the first item for the current product
    //Updating the number for the current product 
    var newNum = Number($.cookie("Product-"+productToAdd)) + 1;
    $.cookie("Product-"+productToAdd, newNum);
  } else { //If this the first item for the current product
    //Creating the first number for the current product
    $.cookie("Product-"+productToAdd, 1);
  }
}); //end click function for Order Button

在购物车页面上,我需要列出每个已保存值的 cookie 的产品名称。因此,我需要能够获取 cookie 的键名,以便在数据库中查找产品名称。

如何获得一个包含所有当前 javascript cookie 的键的数组?

4

1 回答 1

0

您可以列出当前域的 cookie:

function listCookies() {
    var theCookies = document.cookie.split(';');
    var aString = '';
    for (var i = 1 ; i <= theCookies.length; i++) {
        aString += i + ' ' + theCookies[i-1] + "\n";
    }
    return aString;
}

但出于安全原因,您不能列出其他域的 cookie

DixonD 在如何使用 Javascript 列出当前页面的所有 cookie?

于 2013-04-02T04:48:15.937 回答