Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.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 knockoutJS中绑定到可观察数组的问题_Javascript_Jquery_Arrays_Knockout.js_Ko.observablearray - Fatal编程技术网

Javascript knockoutJS中绑定到可观察数组的问题

Javascript knockoutJS中绑定到可观察数组的问题,javascript,jquery,arrays,knockout.js,ko.observablearray,Javascript,Jquery,Arrays,Knockout.js,Ko.observablearray,我目前在将数据绑定到knockoutJS中的可观察数组时遇到问题。我试图做的是根据用户从选择框中的选择显示新值 小提琴可在 我的HTML如下所示 <select data-bind="options: categories, optionsText: 'name', optionsValue: 'id', value: selectedCategory, optionsCaption: 'Choose...', event: { change: c

我目前在将数据绑定到knockoutJS中的可观察数组时遇到问题。我试图做的是根据用户从选择框中的选择显示新值

小提琴可在

我的HTML如下所示

<select data-bind="options: categories, 
    optionsText: 'name',
    optionsValue: 'id',
    value: selectedCategory,
    optionsCaption: 'Choose...',
    event: { change: categoryChanged }
    ">
<div data-bind="foreach: values">
    <div data-bind="text: name"></div>
</div>
<div data-bind="foreach: categories">
    <div data-bind="text: name"></div>
</div>
var categories = [
    { "name" : "color", "id": "1" },
    { "name" : "names", "id": "2" }
];
var values0 = [ { "name" : "empty1" }, { "name" : "empty2" } ];
var values1 = [ { "name" : "white" }, { "name" : "black" } ];
var values2 = [ { "name" : "john" }, { "name" : "name" } ];
var Category = function(data) {
    this.name = ko.observable(data.name);
    this.id =  ko.observable(data.id);
};
var Value = function(data) {
    this.name = ko.observable(data.name);
}
var ViewModel = function(categories, values) {
    var self = this;
    self.categories = ko.observableArray(ko.utils.arrayMap(categories, function(category) {
        return new Category(category);
    }));
    self.selectedCategory = ko.observable();
    self.values = ko.observableArray(ko.utils.arrayMap(values, function(value) {
        return new Value(value);
    }));
    self.categoryChanged = function(obj, event) {
        if(self.selectedCategory()) {
            console.log(self.selectedCategory());
            if("1" == self.selectedCategory()) {
                //console.log(values1);
                self.values.push(new Value({"name":"test1"}));
            } else if("2" == self.selectedCategory()) {
                //console.log(values2);
                self.values.push(new Value({"name":"test2"}));
            }
        }
    };
};
var viewModel;
$(document).ready(function() { 
    viewModel = new ViewModel(categories, values0);
    ko.applyBindings(viewModel);
});
当一个类别被改变时,我真正想做的是这样的事情

self.values.removeAll();
for(var v in values1) {
 self.values.push(new Value(v));
}
但这不起作用,所以我只需要将一个新值推送到可观察数组中

此外,我在div上对
类别
的迭代没有显示,我不确定原因

你知道我做错了什么吗?

你的
元素缺少一个结束标记,导致视图中的问题进一步恶化

<select data-bind="options: categories, 
    optionsText: 'name',
    optionsValue: 'id',
    value: selectedCategory,
    optionsCaption: 'Choose...',
    event: { change: categoryChanged }"></select>

更新小提琴: