Select的淘汰验证



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

刚刚将其更改为以下内容:

self.names=ko.oobservable(未定义).extend({必需:true});

最新更新