knockout select绑定,选中默认值

    <script type="text/javascript">
$(function(){
var viewModel =function() {
var self = this;
self.departmentList = ko.observableArray([{"departmentID": "1","departmentName": "信息技术部"},{"departmentID": "2","departmentName": "财务部"}])

self.DepartmentSelected = ko.observable();
self.DepartmentSelect = ko.computed(function () {
if (self.DepartmentSelected() != null || self.DepartmentSelected()!= undefined)
{
console.log(self.DepartmentSelected());
}            
}, self);
};
var vm = new viewModel();
ko.applyBindings(vm);

   /*设置默认项*/
setOptions();
function setOptions()
{
vm.DepartmentSelected(2);
}
});
</script>

 <select data-bind="options:departmentList,optionsText:'departmentName',optionsValue:'departmentID',

value:DepartmentSelected,optionsCaption:'请选择'"></select>

你可能感兴趣的:(绑定,select,knockout,设)