0

必須フィールドであるいくつかの選択コントロールと入力ボックスがフォームにあり、それらを検証するためにノックアウト検証を使用しています。送信時または変更時に (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({ 必須: true });

于 2013-08-16T20:22:55.437 に答える