Cal*_*ass 14 knockout.js knockout-validation
我想select在我的视图中为一个元素添加一个CSS类,我的视图模型有一个我使用Knockout-Validation扩展的属性:
self.selectedRootCause = ko.observable().extend({
required: true
});
Run Code Online (Sandbox Code Playgroud)
然后我select是这样的:
<form data-bind="submit: closeComplaint" method="post">
<select data-bind="options: rootCauses,
optionsText: 'RootCauseText',
value: selectedRootCause,
optionsCaption: 'Choose..',
validationOptions: { errorElementClass:
'input-validation-error' }">
</select>
<input type="submit" value="Close Complaint" />
</form>
Run Code Online (Sandbox Code Playgroud)
我的closeComplaint功能看起来像这样:
self.closeComplaint = function () {
if (self.errors().length == 0) {
$.ajax({
url: '@Url.Action("CloseComplaint")',
data: new DetailsComplaintAdmin(self.currentComplaint(),
self.selectedRootCause().RootCauseId
),
success: function (data) {
console.log(data);
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
只是为了完成,这是我的self.errors()功能:
self.errors = ko.validation.group(self);
Run Code Online (Sandbox Code Playgroud)
问题是,当我提交表单时,类input-validation-error似乎没有添加到我的select输入中?有任何想法吗?
Luf*_*ffy 22
查看此链接
它说,你必须设置 decorateElement到true了应用CSS类的输入标签.
因此,当我全局应用该参数时,它可以工作:
ko.validation.configure({
decorateElement : true
});
Run Code Online (Sandbox Code Playgroud)
看看这个jsfiddle 演示
注意:在Knockout Validation库的更新版本中,decorateElement配置选项已重命名为decorateInputElement(详细信息)
| 归档时间: |
|
| 查看次数: |
12156 次 |
| 最近记录: |