Knockout.js-为foreach中的每个项目添加数量选项

Knockout.js-为foreach中的每个项目添加数量选项,knockout.js,Knockout.js,我有一个可观察的数组“products”,其中包括products对象,products对象用foreach绑定打印。我想要的是在每个产品行旁边插入一个输入框,以及“添加到购物车”按钮,以便用户可以选择每个产品的数量以及要添加到购物车的所有这些信息。我怎样才能做到这一点 请帮忙,我是knockout.js的新手。在这里摆弄:您需要向产品对象添加一个数量: var Product = function (id, name, price) { this.id = id; this.n

我有一个可观察的数组“products”,其中包括products对象,products对象用
foreach
绑定打印。我想要的是在每个产品行旁边插入一个输入框,以及“添加到购物车”按钮,以便用户可以选择每个产品的数量以及要添加到购物车的所有这些信息。我怎样才能做到这一点


请帮忙,我是knockout.js的新手。在这里摆弄:

您需要向产品对象添加一个
数量:

var Product = function (id, name, price)
{
    this.id = id;
    this.name = name;
    this.price = price;
    this.quantity = ko.observable(1);   
};
然后,您需要在输入中使用,以便能够编辑
数量

<input id="quantity" type="number" min="1" data-bind="value: quantity" />
演示


注意:我还修复了第二个表中的
removeFromCart
和错误的属性名称。

上有多个示例,说明了如何执行此操作。有没有检查过?
self.addToCart = function (product) {
  self.orderedItems.push(
   new orderedItem(product.id, product.name, product.price, product.quantity()));
};