我在淘汰赛中选择标签上的值绑定存在一些问题。
我有以下标记:
<select id="faultCode" data-bind="options: FaultCodes, optionsText: 'Description', value: FaultCode, optionsCaption: 'Choose a Fault Code'"></select>
<select id="causeCode" data-bind="options: CauseCodes, optionsText: 'Description', value: CauseCode, optionsCaption: 'Choose a Cause Code'"></select>
<select id="serviceAction" data-bind="options: ActionCodes, value: ActionCode, optionsText: 'Description', optionsCaption: 'Choose an Action Code'"></select>
<select id="plantClass" data-bind="options: PlantClasses, value: PlantClass, optionsText: 'Description', optionsCaption: 'Choose a Plant Class'"></select>
<select id="plantItem" data-bind="options: PlantItems, value: PlantItem, optionsText: 'Description', optionsCaption: 'Choose a Plant Item'"></select>
还有我的 Javascript:
self.FaultCode = ko.observable();
self.ActionCode = ko.observable();
self.PlantClass = ko.observable();
self.PlantItem = ko.observable();
self.CauseCode = ko.observable();
self.FaultCodes = ko.observableArray();
self.ActionCodes = ko.observableArray();
self.PlantClasses = ko.observableArray();
self.PlantItems = ko.observableArray();
self.CauseCodes = ko.observableArray();
self.closeRequest = function () {
var fault = "";
var action = "";
var cause = "";
var pc = "";
var pi = "";
if (self.FaultCode() != undefined) {
fault = self.FaultCode();
}
if (self.ActionCode() != undefined) {
action = self.ActionCode();
}
if (self.CauseCode() != undefined) {
cause = self.CauseCode();
}
if (self.PlantClass() != undefined) {
pc = self.PlantClass();
}
if (self.PlantItem() != undefined) {
pi = self.PlantItem();
}
}
如果用户从所有 5 个选择框中选择一个选项并触发 closeRequest 函数,则 FaultCode observable 的值为 ""(空字符串), CauseCode observable 的值为 undefined。其他三个值 observables 都具有正确的对象作为它们的值。