1

我正在尝试设置一个可以禁用输入的淘汰赛 observable,如果其他两个输入不在 1 到 30 之间。现在当我在 jsFiddle 中运行代码时,我的输入被禁用。不幸的是,我永远无法重新启用输入。这是jsFiddle上的代码 感谢您的帮助。

HTML

<!-- This is a *view* - HTML markup that defines the appearance of your
UI -->
<p>Alcohol Days:
    <input data-bind="value: alcoholDays" />
</p>
<p>Alcohol 5+ Days:
    <input data-bind="value: alcoholFivePlusDays" />
</p>
<p>Alcohol 4- Days:
    <input data-bind="value: alcoholFourLessDays" />
</p>
<p>Drug Days:
    <input data-bind="value: drugDays" />
</p>
<p>Both Days:
    <input data-bind="value: bothDays, enable: enableBothDays" />
</p>
<p>Enable Both Days: <strong data-bind="text: enableBothDays"></strong>

</p>
<p>Alcohol Days: <strong data-bind="text: alcoholDays"></strong>

</p>
<p>Drug Days: <strong data-bind="text: drugDays"></strong>

</p>
<button data-bind="click: capitalizeLastName">Go caps</button>

javascript

// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI
function AppViewModel() {

    var self = this;

    self.alcoholDays = ko.observable("");
    self.alcoholFivePlusDays = ko.observable("");
    self.alcoholFourLessDays = ko.observable("");
    self.drugDays = ko.observable("");
    self.bothDays = ko.observable("");

    self.enableBothDays = ko.computed(function () {

        if ((parseInt(self.alcoholDays) > 0 && parseInt(self.alcoholDays) <= 30) && (parseInt(self.drugDays) > 0 && parseInt(self.drugDays) <= 30)) {
            return true;
        } else {
            return false;
        }

    }, self);

}

// Activates knockout.js
ko.applyBindings(new AppViewModel());

韦德

4

3 回答 3

1

嗨检查这个小提琴 我解决了这个问题

1.) 清除小提琴中的绑定错误

2.) 重构计算 observable

   self.enableBothDays = ko.computed({
    read: function() {
        alert('In');
        var alcDays = Number(self.alcoholDays());
        var drgDays = Number(self.drugDays());
        alert(alcDays+','+drgDays);
        var temp = false;
  if (alcDays > 0 && alcDays <= 30 && drgDays > 0 && drgDays <= 30) {
        temp = true;
    } else {
        temp = false;
    }
        alert(temp);
        return temp;
}
});

3.) 改变启用条件

小提琴..

将其标记为答案

于 2013-02-21T03:59:53.220 回答
1

固定小提琴 http://jsfiddle.net/SNv6n/20/

您调用的是 self.alcoholDays 而不是 self.alcoholDays()。如果您将括号添加到计算中的那些调用并添加函数“capitalizeLastName”,它就可以工作。

self.capitalizeLastName = function () {
     alert('TODO');   
}
self.enableBothDays = ko.computed(function () {

    if ((parseInt(self.alcoholDays()) > 0 && parseInt(self.alcoholDays()) <= 30) && (parseInt(self.drugDays()) > 0 && parseInt(self.drugDays()) <= 30)) {
        return true;
    } else {
        return false;
    }

}, self);
于 2013-02-21T04:09:48.113 回答
0

使用此启用:enableBothDays()== true 或启用:enableBothDays== true

于 2013-02-21T03:15:32.487 回答