Knockout.js 敲除js确保一个值大于另一个值
我有以下业务要求: 有两个文本框,用户需要在其中输入当前年龄和退休年龄。 退休年龄需要大于当前年龄,因此如果用户输入的退休年龄小于当前年龄,则应将退休年龄更新为当前年龄;如果用户输入的当前年龄大于退休年龄,则退休年龄必须设置为当前年龄 有没有一种简单的方法可以使用knockout js实现这一点?我假设两个字段都需要一个计算的可观测值,并有某种后备存储 以下是我的出发点: js: html:Knockout.js 敲除js确保一个值大于另一个值,knockout.js,Knockout.js,我有以下业务要求: 有两个文本框,用户需要在其中输入当前年龄和退休年龄。 退休年龄需要大于当前年龄,因此如果用户输入的退休年龄小于当前年龄,则应将退休年龄更新为当前年龄;如果用户输入的当前年龄大于退休年龄,则退休年龄必须设置为当前年龄 有没有一种简单的方法可以使用knockout js实现这一点?我假设两个字段都需要一个计算的可观测值,并有某种后备存储 以下是我的出发点: js: html: <div class='liveExample'> <p>Curr
<div class='liveExample'>
<p>Current Age: <input data-bind='value: currentAge' /></p>
<p>Retirement Age: <input data-bind='value: retirementAge' /></p>
</div>
当前年龄:
退休年龄:
是的,这就是你想要的:
function MyViewModel() {
var self = this;
var age= ko.observable(32);
var retAge = ko.observable(44);
self.currentAge = ko.computed({
read: function () {
return age();
},
write: function (value) {
retAge(Math.max(value, retAge));
age(value);
},
owner: this
});
}
并以同样的方式创建退休年龄的计算可观测值
function MyViewModel() {
var self = this;
var age= ko.observable(32);
var retAge = ko.observable(44);
self.currentAge = ko.computed({
read: function () {
return age();
},
write: function (value) {
retAge(Math.max(value, retAge));
age(value);
},
owner: this
});
}