使用淘汰赛验证器添加CSS类 [英] Add CSS Class with Knockout Validator

查看:44
本文介绍了使用淘汰赛验证器添加CSS类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在视图中的select元素中添加CSS类,我的视图模型具有使用Knockout-Validation扩展的属性:

I want to add a CSS Class to a select element in my view, my view model has a property which I've extended using Knockout-Validation:

self.selectedRootCause = ko.observable().extend({
    required: true
});

然后我的select就像这样:

<form data-bind="submit: closeComplaint" method="post"> 
    <select data-bind="options: rootCauses, 
                            optionsText: 'RootCauseText', 
                            value: selectedRootCause, 
                            optionsCaption: 'Choose..',
                            validationOptions: { errorElementClass: 
                                                 'input-validation-error' }">
    </select>

    <input type="submit" value="Close Complaint" />
</form>

我的closeComplaint函数如下所示:

self.closeComplaint = function () {
    if (self.errors().length == 0) {
        $.ajax({
            url: '@Url.Action("CloseComplaint")',
            data: new DetailsComplaintAdmin(self.currentComplaint(),
                                        self.selectedRootCause().RootCauseId
                ),
            success: function (data) {
                console.log(data);
            }
        });
    }
}

仅需完成,这是我的self.errors()函数:

Just for completion, here is my self.errors() function:

self.errors = ko.validation.group(self);

问题是提交表单时似乎没有将类input-validation-error添加到我的select输入中?有什么想法吗?

The problem is the class input-validation-error doesn't appear to be added to my select input when I submit my form? Any ideas?

推荐答案

签出此链接

它表示必须将decorateElement设置为true才能应用CSS 输入标签的类.
因此,当我全局应用该参数时,它将起作用:

Its said that you have to set decorateElement to true for applying CSS classes to input tags.
So when i apply that parameter globally it works :

ko.validation.configure({ 
    decorateElement : true
});

检出此jsfiddle 演示

Checkout this jsfiddle demo

注意:在最新版本的Knockout验证库中,decorateElement配置选项已重命名为decorateInputElement(

Note: In more recent versions of the Knockout Validation library, the decorateElement configuration option has been renamed to decorateInputElement(details)

这篇关于使用淘汰赛验证器添加CSS类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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