简体   繁体   English

淘汰赛验证选择列表值

[英]Knockout validation select list values

I have a select list for states with the first value of the array being "-" so that the user must change it and not accidentally submit the form with 'AK' being the default first input. 我有一个状态的选择列表,该数组的第一个值为“-”,以便用户必须更改它,而不会意外提交带有“ AK”作为默认第一个输入的表单。 The problem is as soon as the form loads the "Please choose another value" error is showing because the initial value is flagged. 问题在于表单加载后会立即显示“请选择另一个值”错误,因为已标记了初始值。

<td>State</td><td><select data-bind="options: $root.stateList, optionsText: 'state', optionsValue: 'state', value: selectedState"></select> </td>

Here's the corresponding js 这是对应的js

self.selectedState = ko.observable("").extend({ notEqual: "-" });
self.stateList = [{state: "-"},{state: 'AK'}, {state:'AL'}....{state:'WY'}];

Is there a better way to have a "blank" first value in the list so that it's not set to an incorrect input as soon as the form loads? 有没有更好的方法在列表中具有“空白”的第一个值,以便在表单加载后不将其设置为不正确的输入? If so, does a simple .extend({ required: true }); 如果是这样,请执行一个简单的.extend({ required: true }); handle the valdiation for it? 为它办理检定吗?

Is there a better way to have a "blank" first value in the list so that it's not set to an incorrect input as soon as the form loads? 有没有更好的方法在列表中具有“空白”的第一个值,以便在表单加载后不将其设置为不正确的输入?

Yes. 是。

Look at the optionsCaption parameter . 查看optionsCaption参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM