1

我是 javascript 和淘汰赛的新手。我正在使用淘汰赛验证进行客户端验证,但遇到了一些麻烦。我希望需要一些用户输入的文本框在模糊时显示错误消息(即使用户没有输入任何内容)。我遇到的一个问题是我不希望错误消息立即出现。我能够做到这一点,但想知道是否有人有更优雅的方式来做到这一点。我所做的伪代码是将文本框的值设置为可观察的,然后将其订阅到文本框的 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/

我想知道是否有更好的方法来实现此功能(或任何其他不太复杂的方法)。提前致谢!

4

1 回答 1

0

您只需要在绑定中指定 onvalueUpdate选项。然后敲除也会更新触发验证的模糊事件的值:'blur'valuefirstName

<input type="text" runat="server" ID="FirstName" 
       data-bind="value: firstName, valueUpdate: 'blur'"/>

演示JSFiddle

于 2013-08-22T11:05:25.177 回答