>我有两个下拉列表。根据第一个下拉列表的选定值,我需要填充第二个下拉列表。
我的网页代码:
<select class="form-control" data-bind="options: $root.schemas, optionsText: 'schema', value: selschema"></select>
<!-- ko switch: selschema -->
<!-- ko case: ['Test', 'Another Test'] -->
<select class="form-control" data-bind="options: $root.testschemas, optionsText: 'schema', value: selschematest"></select>
<!-- /ko -->
<!-- ko case: $else -->
<select class="form-control" data-bind="options: $root.othertestschemas, optionsText: 'schema', value: selschematest"></select>
<!-- /ko -->
<!-- /ko -->
我的 js 代码
function TestViewModel() {
var self = this;
self.selschema=ko.observable();
self.selschematest=ko.observable();
self.schemas = ko.observableArray([{ "schema": "Test" }, { "schema": "Another Test" }, { "schema": "Other Test" }]);
self.testschemas = ko.observableArray([{ "schema": "Test1" }, { "schema": "Test2" }]);
self.othertestschemas = ko.observableArray([{ "schema": "other Test1" }, { "schema": "other Test2" }]);
}
ko.applyBindings(new TestViewModel());
Jsfiddle:
我正在尝试使用迈克尔贝斯特的敲除开关案例,但它总是在 else 语句中。
任何帮助都真诚地感谢
谢谢
<!-- ko switch: selschema().schema -->
解决了这个问题..在HTML中添加<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>
为我指明了方法。
JSFIDDLE: