淘汰赛验证 hello world 未在 jsfiddle 上运行 [英] Knockout validation hello world not running on jsfiddle

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

问题描述

我正在尝试询问有关在页面加载时运行的异步击倒.js 验证的问题,并且我正在尝试在 jsfiddle 上重现该问题.

问题是,尽管我的机器上运行了更复杂的场景,但我无法让最基本的示例工作.这有什么问题吗?

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

我必须发布代码:

<input type="text" data-bind="value:validatableField"/><p data-bind="validationMessage:validatableField"></p><button data-bind="click: alertValue">值被警告正常,但没有验证</button>

var Vm = 函数(){var self = this;self.validatableField = ko.observable().extend({ 等于: 2 });self.alertValue = 函数(){警报(self.validatableField());};};ko.applyBindings(new Vm(), document.getElementById('vm'));

解决方案

您的代码没有任何问题.

但是当前版本的验证插件 (cdnjs 1.0.2) 已经很老了,它有一个错误,阻止了 ko.validation.registerExtenders 正常工作.此错误 此后已修复.

作为一种解决方法,您需要在小提琴开始时调用 ko.validation.registerExtenders():

ko.validation.registerExtenders();var Vm = 函数(){var self = this;self.validatableField = ko.observable().extend({ equal: "2" });self.alertValue = 函数(){警报(self.validatableField());};};ko.applyBindings(new Vm(), document.getElementById('vm'));

演示 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/

I have to post code:

<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'));

解决方案

There is nothing wrong with your code.

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.

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'));

Demo JSFiddle.

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

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