我可以只在元素中添加HTML5属性以进行连线验证吗?
如果是这样,它似乎对我不起作用。我需要做些额外的事情吗?
http://jsfiddle.net/FcM2A/1/
vm required<input data-bind="value: num1, valueUpdate: 'afterkeydown'"/><br/>
HTML5 required<input required="true" data-bind="value: num2, valueUpdate: 'afterkeydown'" /><br/>
num1 isValid: <span data-bind="text: num1.isValid()"></span><br/>
num2 isValid: <span data-bind="text: num2.isValid()"></span>
<p data-bind="text:ko.toJSON($root)"></p>
ko.validation.init( {parseInputAttributes: true, writeInputAttributes: true} ); //enable HTML5 validation, write HTML5 validation attributes to the controls
var viewModel = {
num1: ko.observable("1").extend({ required: true }),
num2: ko.observable("2")
};
ko.applyBindings(viewModel);
最佳答案
在这里尝试:http://jsfiddle.net/FcM2A/29/
您不见了:
ko.validatedObservable(viewModel);
vm required<input data-bind="value: num1, valueUpdate: 'afterkeydown'"/><br/>
HTML5 required<input required="true" data-bind="value: num2, valueUpdate: 'afterkeydown'" /><br/>
num1 isValid: <span data-bind="text: num1.isValid()"></span><br/>
num2 isValid: <span data-bind="text: num2.isValid()"></span>
<p data-bind="text:ko.toJSON($root)"></p>
ko.validation.init( {parseInputAttributes: true, writeInputAttributes: true} ); //enable HTML5 validation, write HTML5 validation attributes to the controls
var viewModel = {
num1: ko.observable("1").extend({ required: true }),
num2: ko.observable("2")
};
ko.validatedObservable(viewModel);
ko.applyBindings(viewModel);
关于knockout.js - 使用HTML5属性进行 knockout 验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15031119/