javascript - jQuery 中的动态数组

标签 javascript jquery arrays dynamic

我遇到了一个问题:

if($.cookie('products') == undefined) {
    $.cookie("products", [$(this).data('name')]);
}
else $.cookie("products", [$.cookie('products')+$(this).data('name')]);

我的目标是将产品名称添加到 cookie,因为我需要在通过表单发送产品名称后在 PHP 中使用它们。

我正在使用数组,因为在代码中我需要删除最后一个对象:

$.cookie("products", $.cookie('products').pop());

或者也许您有另一种解决方案将变量从一个文件中的 jQuery 传输到另一个文件中的 PHP 变量?

最佳答案

好吧,我用这个解决了主要问题:

var products = [$.cookie('products')];
products.push($(this).data('name'));
$.cookie("products", products);

但是 pop() 方法仍然存在问题:

products.pop();
$.cookie("products", products);

不是只删除最后一个元素,而是删除整个数组。

关于javascript - jQuery 中的动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17775195/

相关文章:

javascript - 使用通配符作为路径的深度路径查询

javascript - getElementById 没有返回它应该返回的内容

c - 如何解释这段代码 : if (strstr(line, *argv) != EOF) != except

ios - Realm 列表和 Swift 数组

java - 这哪里出了问题?线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException : 5

javascript - 我想将光标放在文本框 onfocus 的开头

javascript - adminLTE 2.4.5 treeview 刷新后无法正常工作,仅在 rails 中工作

javascript - 如何根据单选按钮选择更改文本输入的最大长度验证?

Javascript 图像高亮(解释)

javascript - 如何知道文本框何时更改(无需等待模糊)?