Knockout.js中的不确定值

时间:2018-12-01 13:11:59

标签: knockout.js

我已经定义了类似self.loadAffected = function(){ko.observable(true);}

的函数

并尝试在其他功能上打印

self.loadAffectedChanged = function() {
    // clear "Road Blocked" when Load Affected is selected
    **alert(self.loadAffected());**
    if (self.loadAffected === false)
        self.groupable(false);
    else
    // clear "Groupable" when Load Affected is cleared

        self.roadBlocked(false);
};

通过类似HTML的方法调用上述功能

<input type="checkbox" data-bind="jqmChecked: loadAffected, click: loadAffectedChanged" 
                   name="load_affected" id="load_affected"/>

当我单击复选框时,它抛出未定义为弹出框的消息, enter image description here

有人可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

self.loadAffected()将始终为您提供undefined,因为它只是一个不返回任何内容的函数。我不确定custom binding jqmChecked的用途,但与该问题无关。

您应该将代码更改为:

self.loadAffected = ko.observable(true); // now loadAffected is an observable

self.loadAffectedChanged = function() {
    // you need to add the parentheses here to read the value of loadAffected observable
    if (self.loadAffected() === false)
        self.groupable(false);
    else
    // clear "Groupable" when Load Affected is cleared

     self.roadBlocked(false);
};