0

更新代码:

http://jsfiddle.net/S96pA/1/

<!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>
​

预期,该selected项目被突出显示(选择),但没有任何反应。为什么?

4

1 回答 1

1

您必须为您的选择添加value绑定:

<select data-bind="options: myList, value: selected" multiple="multiple" size="5">

viewModel 也是一个函数,所以你必须创建它的实例:

ko.applyBindings(new viewModel());

这是工作小提琴:http: //jsfiddle.net/wM3EX/4/

编辑

更改出价顺序optionsselectedOptions出价。选项应该始终是第一个:

<select size="10" multiple="true"
        data-bind="options: AttachmentSelect, selectedOptions: Attachment">
</select>

工作小提琴:http: //jsfiddle.net/S96pA/2/

于 2012-12-11T11:17:54.010 回答