我不确定这是不是一个bug,但是当1.2.1这样做时,knockout.js 2.1对这个样本不起作用。 http://jsfiddle.net/gurkavcu/kKNtv/
更新外部资源以使用2.1并停止工作: http://cloud.github.com/downloads/SteveSanderson/knockout/knockout-2.1.0.js
dataList = [ {name:'length1',id:1},
{name:'length2',id:2},
{name:'length3',id:3},
{name:'length4',id:4},
{name:'length5',id:5} ]
var editOfferViewModel = {
selectedLength: ko.observable(),
multiSelectedLength: ko.observableArray()
};
ko.applyBindings(editOfferViewModel);
$(document).ready(function() {
editOfferViewModel.selectedLength(2);
editOfferViewModel.multiSelectedLength(['2','3']);
});
答案 0 :(得分:1)
它绑定到id值,这是一个整数,但你告诉它字符串。也许这种行为在淘汰赛1和2之间发生了变化。如果您将选定的值设置为整数或者创建id属性字符串,它将起作用。这是一个简单的示例,只显示所有选定项目的JSON,您可以看到它包含字符串,选择框不显示任何内容,然后当您选择项目时,它们显示为整数(fiddle)。这两个变化都可以起作用:
dataList = [ {name:'length1',id:1},{name:'length2',id:2},{name:'length3',id:3},{name:'length4',id:4},{name:'length5',id:5} ]
// dataList = [ {name:'length1',id:'1'},{name:'length2',id:'2'},{name:'length3',id:'3'},{name:'length4',id:'4'},{name:'length5',id:'5'} ] // this would work
...
editOfferViewModel.multiSelectedLength(['2','3']);
// editOfferViewModel.multiSelectedLength([2,3]); // this works
我的问题就是为什么这一切都有用......