Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 淘汰赛不起作用?_Javascript_Knockout.js - Fatal编程技术网

Javascript 淘汰赛不起作用?

Javascript 淘汰赛不起作用?,javascript,knockout.js,Javascript,Knockout.js,我使用的是knockoutjs,我是新手。我想根据下拉列表选择的值更改模型数据。因此,在我的AppModel中,我订阅了我想要更改的数组。但它不起作用?。这是我的密码: var filteredStocks = []; function viewModel(model) { this.isGeneral = ko.observable(model.generalStockEnabled); this.stocks = ko.observable();;

我使用的是
knockoutjs
,我是新手。我想根据下拉列表选择的值更改模型数据。因此,在我的AppModel中,我订阅了我想要更改的数组。但它不起作用?。这是我的密码:

var filteredStocks  = [];
function viewModel(model) {
        this.isGeneral = ko.observable(model.generalStockEnabled);
        this.stocks = ko.observable();;
        if (model.generalStockEnabled === true)
        {
            filteredStocks = $.grep(model.stocks, function (v) {
                return v.sourceID === -1;
            });
        }
        else
        {
            filteredStocks = $.grep(model.stocks, function (v) {
                return v.sourceID !== -1;
            });
        }
        // If drop downlist changed
        var dropDownListSelectedValue = $('#enableGeneratInventorydl :selected').val();
        this.stocks.subscribe(function () {
            if (dropDownListSelectedValue === "True") {
                filteredStocks = $.grep(model.stocks, function (v) {
                    return v.sourceID === -1;
                });
                this.stocks(filteredStocks)
                this.isGeneral(true);
            }
            else
            {
                filteredStocks = $.grep(model.stocks, function (v) {
                    return v.sourceID !== -1;
                });
                this.stocks(filteredStocks)
                this.isGeneral(false);
            }
        }, this);

        this.stocks = ko.observableArray(filteredStocks);
当我更改下拉列表值时。股票价值保持不变


感谢您的帮助

出现此问题是因为您正在将
stocks
变量重新分配给另一个可观察变量。 所以你首先要做的是:

this.stocks = ko.observable();
然后订阅这个可观察的。但后来你做到了:

this.stocks = ko.observableArray(filteredStocks);
这将
股票
与另一个可观察到的股票相关联。订阅将针对原始可观察对象,即第一次分配中的可观察对象。 请参阅此提琴,了解一个简短的示例:

解决方案是替换
this.stocks=ko.observable()

使用
this.stocks=ko.observableArray()

并替换
this.stocks=ko.observatarray(filteredStocks)

使用
this.stocks(filteredStocks)