0

我的表单中有一些选择控件和输入框,它们是必填字段,我正在使用 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/

4

1 回答 1

0

只需将其更改为以下内容:

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

于 2013-08-16T20:22:55.437 回答