我有2個下拉列表。當我從一個下拉列表中選擇值時,它會根據選擇的內容在另一個下拉列表中加載值。從另一個下拉列表中選擇值時從下拉列表中加載所選值
這裏是我的html代碼
<select data-bind="options: filters, value: filter"></select>
<select data-bind="options: filteredItems, optionsText: 'name'"></select>
這裏是我的淘汰賽代碼
var ViewModel = function(data) {
var self = this;
self.filters = ko.observableArray(data.filters);
self.filter = ko.observable('');
self.items = ko.observableArray(data.items);
self.filteredItems = ko.computed(function() {
var filter = self.filter();
if (!filter || filter == "None") {
return self.items.slice(0, 6);
} else {
return self.items.slice(2);
}
});
};
var initialData = {
filters: ["None", "Old", "New", "Super"],
items: [{ name: "Corvette", type: "Old"},
{ name: "Charger", type: "Old"},
{ name: "Prius", type: "New"},
{ name: "Magnum", type: "New"},
{ name: "McLaren", type: "Super"},
{ name: "Saleen", type: "Super"}]
ko.applyBindings(new ViewModel(initialData));
當我選擇類型爲無則選擇所有的汽車,如果我選擇無以外的類型,那麼它應該只選擇「充電器」和「Magnum」
這是鏈接到fiddle
很好,小提琴似乎根據你想要的東西的工作,不是嗎? – Jalayn 2013-04-25 17:30:22
@Jalayn y小提琴加載前2個值,當我選擇類型非無 – DevelopmentIsMyPassion 2013-04-25 17:52:25
與我的更新它是否適合你? – Jalayn 2013-04-25 18:10:15