尝试将数据绑定到下拉列表,
function EmailTemplate(data) {
var self = this;
self.etId = ko.observable(data.email_template_id);
self.etTypeId = ko.observable(data.email_template_type_id);
self.etTitle = ko.observable(data.email_template_title);
self.etContent = ko.observable(data.email_template_content);
self.etAppYear = ko.observable(data.app_year);
self.etSubject = ko.observable(data.subject);
self.etActive = ko.observable(data.active);
}
function EmailTemplateViewModel() {
var self = this;
self.ETList = ko.observableArray();
var uri = '/admin/services/EmailTemplateService.svc/EmailTemplates';
OData.read(uri, function (data, response) {
$.each(data.results, function (index, item) {
self.ETList.push(new EmailTemplate(item));
});
});
}
$(document).ready(function () {
ko.applyBindings(new EmailTemplateViewModel());
});
HTML 标记:
<select data-bind="options: ETList, value:etId, optionsText: 'etTitle' "class="dropdown"></select>
当我运行它时,我得到:未捕获的错误:无法解析绑定。消息:ReferenceError:etIdis 未定义;绑定值:选项:ETList,值:etId,optionsText:'etTitle'
当我们绑定到下拉列表时,我们应该如何绑定值?在绑定之后,我们应该如何在 Knockout 中捕获或创建下拉更改事件?