cuốn sách gpt4 ai đã làm

javascript - 将项目添加到数组中 - 单独

In lại Tác giả: Walker 123 更新时间:2023-11-28 17:04:09 28 4
mua khóa gpt4 Nike

目标:单击按钮并将成分作为单独的项目添加到数组中。

当前设置:

    <% currentUser.ingredients.forEach(function(items){ %> 

<%= items %>


<% }) %>

这给出:蓝莓芒果柠檬汁

然后我希望能够通过单击按钮将成分作为单独的项目添加到数组中:

var allIngredients = []

$("#makeList").click(function() {

allIngredients.push($(".ingredients").text())
console.log(allIngredients)

});

这会打印[“蓝莓芒果柠檬汁”]

问题是这都是一个项目而不是单独的项目。

我需要更改什么才能使它们成为单独的项目?

câu trả lời hay nhất

您需要迭代$('.ingredients')

var allIngredients = [];

$("#makeList").click(function() {

$(".ingredients").each(function() {
allIngredients.push($(this).text());
});

console.log(allIngredients);

});

关于javascript - 将项目添加到数组中 - 单独,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56347649/

28 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress