Javascript 更新列表中的项目

Javascript 更新列表中的项目,javascript,knockout.js,Javascript,Knockout.js,我是击倒赛的初学者 我做了这个页面: 我只想更新sellIt函数返回的项目 我该怎么做呢?您设置的数量属性不正确quantity是一个可观察的ko,因此需要使用以下语法: self.sellIt = function (product) { $.post('/Product/SellIt', { id: product.id }, function (data) { var res = Enumer

我是击倒赛的初学者

我做了这个页面:

我只想更新sellIt函数返回的项目


我该怎么做呢?

您设置的
数量属性不正确
quantity
是一个可观察的
ko
,因此需要使用以下语法:

        self.sellIt = function (product) {
            $.post('/Product/SellIt', { id: product.id },
            function (data) {
                var res = Enumerable.From(self.products)
                            .Where("i => i.id == " + data.Id)
                            .Select("s => s");

                res.quantity(data.Quantity); // this is the important bit!!
            });
        };
但是,我认为您可以将代码缩短为:

        self.sellIt = function (product) {
            $.post('/Product/SellIt', { id: product.id },
            function (data) {
                product.quantity(data.Quantity);
            });
        };

您设置的
数量
属性不正确
quantity
是一个可观察的
ko
,因此需要使用以下语法:

        self.sellIt = function (product) {
            $.post('/Product/SellIt', { id: product.id },
            function (data) {
                var res = Enumerable.From(self.products)
                            .Where("i => i.id == " + data.Id)
                            .Select("s => s");

                res.quantity(data.Quantity); // this is the important bit!!
            });
        };
但是,我认为您可以将代码缩短为:

        self.sellIt = function (product) {
            $.post('/Product/SellIt', { id: product.id },
            function (data) {
                product.quantity(data.Quantity);
            });
        };