禁用基于其他输入的输入Knockoutjs
问题描述:
我想设置一个敲除的observable,将禁用输入,如果两个其他输入不是都在1和30之间。现在,当我在jsFiddle中运行代码时,我的输入被禁用。不幸的是,我永远无法重新启用输入。这里是jsFiddle上的代码感谢您的任何帮助。禁用基于其他输入的输入Knockoutjs
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());
韦德
答
hi check this fiddle我修复了问题。
1)清除了您提琴绑定错误
2)重新计算结构观察到
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)改变启用条件
痕作为回答
答
使用此 启用:enableBothDays()==真 或者 启用:enableBothDays ==真
答
固定小提琴 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);
谢谢布莱恩,不幸的是我只能接受一个答案,并与谁先回答。 – Wade73 2013-02-21 11:10:59