Javascript 单击按钮应将项添加到数组中,但每次函数调用时数组为空
代码看起来像这样Javascript 单击按钮应将项添加到数组中,但每次函数调用时数组为空,javascript,arrays,shopify,liquid,Javascript,Arrays,Shopify,Liquid,代码看起来像这样 let sectionWithLimitItems = []; function addItems(productId) { sectionWithLimitItems.push(productId) } <button onclick='appendItems({{ productId }})'>Click Me</button> let sectionWithLimitItems=[]; 函数附加项(productId){ 部分Wi
let sectionWithLimitItems = [];
function addItems(productId) {
sectionWithLimitItems.push(productId)
}
<button onclick='appendItems({{ productId }})'>Click Me</button>
let sectionWithLimitItems=[];
函数附加项(productId){
部分WithLimitItems.push(产品ID)
}
点击我
使用Shopify的主题模板语言liquid,但不确定这是否会造成复杂性。
如果每次调用函数时都记录数组,则在函数开始时数组为空,可以看到添加了id,但在单击下一步按钮时数组再次为空。有什么想法吗
--编辑-为大家的错误感到抱歉!我的代码中确实有arr.push,而不是append。我发帖子的时候思维不正确。对于sectionWithLimitItems.push(productId)在javascript中,如果您不使用
Array.append()
,则应该使用Array.push()
()。我想您可能正在寻找
const sectionWithLimitItems=[];
函数附加项(productId){
部分WithLimitItems.push(产品ID)
}
点击我
对于Javascript中的数组数据
结构,您可以使用push()
方法添加到数组
的末尾。有关更详细的说明,请查看
您正在查找我的错误代码示例,该示例编写错误,仍然无法使用arr.push。谢谢你指出我的错误,然后请更新你的代码,并添加一个
const sectionWithLimitItems = [];
function appendItems(productId) {
sectionWithLimitItems.push(productId)
}
<button onclick='appendItems({{ productId }})'>Click Me</button>
let sectionWithLimitItems = [];
function appendItems(arr,item) {
arr.push(item)
}
console.log(sectionWithLimitItems)