我是javascript和淘汰赛的新手。我正在使用敲除验证进行客户端验证,并且遇到了一些麻烦。我希望文本框需要一些用户输入来显示模糊的错误消息(即使用户没有输入任何内容)。我遇到的一个问题是我不希望错误消息立即显示出来。我能够让这个工作,但想知道是否有人有一个更优雅的方式来做到这一点。我所做的伪代码是将文本框的值设置为observable,然后将其订阅到文本框的hasfocus。以下是视图模型的示例代码以及随之而来的小提琴:
self.firstName = ko.observable().extend({
required: true,
notify: 'always'
});
self.firstName.focused = ko.observable();
self.firstName.focused.subscribe(function(newVal) {
if(not the first time in the function and the value hasn't changed)
{
update the value to itself;
//if this is empty then it will trigger the "required" error message
}
});
http://jsfiddle.net/sderico/qAnxw/
我想知道是否有更好的方法来实现此功能(或任何其他不太复杂的方式)。提前谢谢!
答案 0 :(得分:0)
您只需在'blur'
绑定中指定valueUpdate
option到value
。然后,淘汰赛还会更新触发验证的模糊事件firstName
的值:
<input type="text" runat="server" ID="FirstName"
data-bind="value: firstName, valueUpdate: 'blur'"/>
演示JSFiddle。