我的表单中有一些选择控件和输入框,这些是必填字段,我使用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*/
}
答案 0 :(得分:0)
刚刚将其更改为以下内容:
self.names = ko.observable(undefined).extend({ 要求:是的 });