javascript - 将项目添加到数组中 - 单独
问题描述
目标:单击一个按钮并将成分作为单独的项目添加到数组中。
当前设置:
<% currentUser.ingredients.forEach(function(items){ %>
<p class='ingredients'> <%= items %> </p>
<% }) %>
这给出了:蓝莓芒果柠檬汁
然后,我希望能够通过单击按钮将成分作为单独的项目添加到数组中:
var allIngredients = []
$("#makeList").click(function() {
allIngredients.push($(".ingredients").text())
console.log(allIngredients)
});
这打印 [“Blueberrys Mango Lemon Juice”]
问题是它是一个项目而不是单独的项目。
我需要更改什么以使它们成为单独的项目?
解决方案
你需要迭代$('.ingredients')
var allIngredients = [];
$("#makeList").click(function() {
$(".ingredients").each(function() {
allIngredients.push($(this).text());
});
console.log(allIngredients);
});
推荐阅读
- sql - 仅显示那些没有特定键限定符的记录
- django - 如何在通用视图中向 Django 模型字段的 help_text 添加样式
- html - 如何组合:在特定浏览器和 file://[unc path] 中打开目标链接
- cmake - 在 CMake 中设置“-D”变量
- pandas - 如何使用带有偏移量的 pd.Grouper?
- directory - 如何列出 Julia 目录中的所有文件和目录?
- gnome - 使用 gnome-terminal 时无法打开文件“/etc/dconf/db/local”错误
- javascript - 提供给“DataProvider”的类型为“number”的无效道具“data”,应为“array”。React-bootstrap-table2,Firebase 实时数据库
- html - 下拉按钮未居中
- events - 如何使用“eventnews”扩展制作“事件”