我有一个控制子类别下拉列表的类别下拉列表。如果所选类别的子类别数组为空,我想隐藏子类别下拉列表。
下面的示例代码:
<script>
self.categories = ko.observableArray([
{"name": "top 1", "subcategories":[
{"name": "sub 1"},
{"name": "sub 2"}
]},
{"name": "top 2", "subcategories":[]}
]);
self.selected_category = ko.observable();
self.selected_sub_category = ko.obserable();
</script>
<div>
<select data-bind="options: categories, optionsText: "name", optionsCaption: "Select", value: selected_category"></select>
</div>
<div data-bind="with:selected_category">
<select data-bind="options: subcategories, optionsText: "name", optionsCaption: "Select", value: selected_sub_category"></select>
</div>