[英]value object not updating when item selected from a select list in knockoutjs
I'm having some problems with the value binding on a select tag in knockout. 我在淘汰赛中的select标签上绑定了值时遇到了一些问题。
I've got the following markup: 我有以下标记:
<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>
And my Javascript: 而我的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();
}
}
If the user chooses an option from all 5 of the select boxes and fires the closeRequest function, the FaultCode observable has a value of "" (Empty String) and the CauseCode observable has a value of undefined. 如果用户从所有5个选择框中选择一个选项并触发closeRequest函数,则FaultCode可观察值的值为“”(空字符串),而CauseCode可观察值的值为undefined。 The three other value observables all have the correct object as their value.
其他三个可观察值均具有正确的对象作为其值。
Are you sure that your FaultCode has a value when you are entering closeRequest or whatever? 当您输入closeRequest或其他内容时,您确定您的FaultCode是否具有值?
console.log(self.FaultCode());
When you first get into that function. 首次使用该功能时。 Also, you are saying if
(self.FaultCode() != undefined)
which is a loose comparison and not very good. 另外,您说的是if
(self.FaultCode() != undefined)
,这是一个宽松的比较,不是很好。 If you are getting a value logged from the above console.log statement, this should fix it for you - 如果您是从上述console.log语句中记录的值,则应该为您解决-
self.closeRequest = function () {
console.log(self.FaultCode());
var fault = "";
var action = "";
var cause = "";
var pc = "";
var pi = "";
if (self.FaultCode()) {
fault = self.FaultCode();
}
if (self.ActionCode()) {
action = self.ActionCode();
}
if (self.CauseCode()) {
cause = self.CauseCode();
}
if (self.PlantClass()) {
pc = self.PlantClass();
}
if (self.PlantItem()) {
pi = self.PlantItem();
}
}
As long as the observables aren't false
, undefined
, ""
, or null
it will set fault equal to the observable. 只要可观察变量不是
false
, undefined
, ""
或null
,它将设置的错误等于可观察变量。
When you use a double equal sign you are comparing the value loosely, use the triple equals for type comparison as well. 当使用双等号时,您将比较松散地比较该值,同时也将三等号用于类型比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.