【问题标题】:Knockout Validations for SelectSelect 的淘汰赛验证
【发布时间】:2013-08-17 04:22:55
【问题描述】:

我的表单中有一些选择控件和输入框,它们是必填字段,我正在使用 Knockout Validations 来验证它们。我希望在提交或修改时突出显示控件(使用 errorElementClass)。不幸的是,当页面首次加载时,我的选择控件被突出显示。

<div>Names
    <select data-bind="options: allNames, optionsValue: 'id', optionsText: 'name', value: names, optionsCaption:'Select Names'"></select>
    <button id="btnSubmit">Submit</div>

            ko.validation.configure({
            insertMessages: false,
            messagesOnModified: true,
            decorateElement: true,
            errorElementClass: "input-validation-error",
            deep: false,
            enableErrorDetails: true
        });

        function ViewModel() {
            var self = this;

            self.names = ko.observable("").extend({
                required: true
            });
            self.allNames = new ko.observableArray([]);

            self.call = function () {
                self.allNames.push({
                    id: 1,
                    name: "Adam"
                });
                self.allNames.push({
                    id: 2,
                    name: "Bert"
                });
                self.allNames.push({
                    id: 3,
                    name: "Keith"
                });
                self.allNames.push({
                    id: 4,
                    name: "Anna"
                });
                self.allNames.push({
                    id: 5,
                    name: "Andie"
                });
            }
        }
        self.errors = ko.validation.group(this);
        vm = new ViewModel();
        vm.call();


        $("#btnSubmit").click(function () {

        });
        ko.applyBindings(vm);

CSS

.input-validation-error {
border-color:red !important;
border-style:solid !important;
border-width:1Px !important;
box-shadow: 0px 0px 2px red !important;
/*Add webkit box shadows for other browsers*/

}

http://jsfiddle.net/ppPVc/2/

【问题讨论】:

  • 您能更详细地描述一下这个问题吗?
  • 我已经修改了我的问题,希望对您有所帮助。谢谢。
  • 酷,我会看看它,但为了将来参考,当您可以显示一个简化的小提琴以帮助更快地理解和调试问题时,它的帮助会提高 100 倍,仅供参考
  • 我编辑了我的问题并添加了一个小提琴。我基本上希望元素在提交时突出显示,而不是在页面加载时突出显示。感谢您的帮助!
  • 所以您不想在按下提交之前应用 CSS?

标签: jquery validation knockout.js


【解决方案1】:

刚刚改成下面这样:

self.names = ko.observable(undefined).extend({ 要求:真 });

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 2011-08-09
    • 2012-11-04
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2012-12-05
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多