knockout.js – 自定义错误消息和位置

栏目: JavaScript · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/18872652/customize-error-message-and-placement
目前, Knockout-Validation

插件会自动将此HTML元素添加到我的网页:

<span class="validationMessage">This field is required.</span>

>我想更改“此字段是必填项”.文本.

>我想更改<span>的位置HTML元素.

>我想在我的文本框中添加一个CSS类(特别是.err),这样我就可以添加一个红色边框.

如何通过Knockout-Validation完成?

您可以在每个属性的基础上进行 change the default messages 验证:
test: ko.observable().extend({
        required: {
            params: true,
            message: "This is required"
        }
    })

您可以使用 validationMessage binding 在任何地方显示错误:

<span data-bind="validationMessage: test"></span>

您可以使用 decorateElement and errorElementClass options (或 other validation bindings )在输入上添加一些自定义类:

ko.validation.init({
    decorateElement: true,
    errorElementClass: 'err'
});

演示 JSFiddle .

翻译自:https://stackoverflow.com/questions/18872652/customize-error-message-and-placement


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Usability for the Web

Usability for the Web

Tom Brinck、Darren Gergle、Scott D. Wood / Morgan Kaufmann / 2001-10-15 / USD 65.95

Every stage in the design of a new web site is an opportunity to meet or miss deadlines and budgetary goals. Every stage is an opportunity to boost or undercut the site's usability. Thi......一起来看看 《Usability for the Web》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具