淘汰赛验证你好世界不在jsfiddle上运行 [英] Knockout validation hello world not running on jsfiddle

查看:94
本文介绍了淘汰赛验证你好世界不在jsfiddle上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个关于在页面加载时运行的async基因敲除.js验证的问题,并且我试图在jsfiddle上重现该问题.

I'm trying to ask a question about async knockout.js validation running on page load, and I'm attempting to reproduce the issue on jsfiddle.

事情是,尽管我的机器上运行着更复杂的场景,但我无法获得最基本的示例.怎么了?

Thing is, I can't get the most basic example to work, despite having a more complex scenario running on my box. What's wrong with this?

http://jsfiddle.net/C5rSm/4/

我必须发布代码:

<div id="vm">
    <input type="text" data-bind="value: validatableField" />
    <p data-bind="validationMessage: validatableField"></p>
    <button data-bind="click: alertValue">value is alerted ok, but doesn't validate</button>
</div>

var Vm = function(){
    var self = this;
    self.validatableField = ko.observable().extend({ equal: 2 });
    self.alertValue = function(){
        alert(self.validatableField());
    };
};

ko.applyBindings(new Vm(), document.getElementById('vm'));

推荐答案

您的代码没有错.

但是((cdnjs 1.0.2 )已经很旧了,并且它有一个错误,导致ko.validation.registerExtenders无法正常工作.从那时起,此错误已得到修复.

However the current version of the validation plugin on (cdnjs 1.0.2) is quite old and it has a bug which prevents the ko.validation.registerExtenders working correctly. This bug has been fixed since then.

作为解决方法,您需要在提琴的开头致电ko.validation.registerExtenders():

As a workaround you need to call ko.validation.registerExtenders() at the start of your fiddle:

ko.validation.registerExtenders();
var Vm = function(){
    var self = this;
    self.validatableField = ko.observable().extend({ equal: "2" });
    self.alertValue = function(){
        alert(self.validatableField());
    };
};

ko.applyBindings(new Vm(), document.getElementById('vm'));

演示 JSFiddle .

这篇关于淘汰赛验证你好世界不在jsfiddle上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆