更新的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script src="http://github.com/downloads/SteveSanderson/knockout/knockout-2.2.0.js" type="text/javascript"></script>
</head>
<body>
<form id="new-pub">
<input data-bind="value: Title" /><br/>
<select size="10" multiple="multiple"
data-bind="selectedOptions: Attachment, options: AttachmentSelect"></select>
</form>
<script type="text/javascript">
var AttachmentSelect=["C2012_info_mail.pdf", "C2012_programme.pdf"];
var viewModel =
{
Title : ko.observable('Hello world'),
Attachment: ko.observableArray([ "C2012_info_mail.pdf" , "C2012_programme.pdf" ])
};
ko.applyBindings(viewModel, document.getElementById("new-pub"));
</script>
</body>
</html>
预计会突出显示({1}}个selected
个项目,但没有任何反应。为什么呢?
答案 0 :(得分:1)
您必须为您的选择添加value
绑定:
<select data-bind="options: myList, value: selected" multiple="multiple" size="5">
viewModel也是一个函数,因此你必须创建它的实例:
ko.applyBindings(new viewModel());
这是工作小提琴:http://jsfiddle.net/wM3EX/4/
修改强>:
更改options
和selectedOptions
出价的顺序。选项应始终是第一个:
<select size="10" multiple="true"
data-bind="options: AttachmentSelect, selectedOptions: Attachment">
</select>