【发布时间】:2014-08-11 18:52:56
【问题描述】:
我遇到了与described in this question 完全相同的问题。但是公认的解决方案并不能解决问题。
这是我的表单元素标记:
<select
class="form-control"
data-bind="options: industries, optionsText: 'description', optionsValue: 'code', optionsCaption: 'Choose...', value: industry_code"
></select>
我的数据如下所示:
[
{"code":"00","description":"Unclassified","parent":null},
{"code":"01","description":"Amusement\/Arcade","parent":null},
...
];
我正在尝试使用以下代码将此字段设为必填:
self.industry_code.extend({
required: { message: "Please select an industry." }
});
它非常适合选择、更新等,但是当我第一次加载表单时,“请选择行业”。出现信息。我的其他淘汰验证代码(<input type='text'> 元素)都没有这样做,所以我希望这个代码的行为方式相同。
我确实尝试了上述问题中提到的解决方案(事实上,我已经在使用optionsCaption 绑定)。也许我错过了一步......the answer to the previous question 模糊到几乎没用的地步。
其他人解决了这个问题吗?
【问题讨论】:
-
你自己写过
requiredextender吗?也许这个解决方案会有用 - stackoverflow.com/questions/17743462/…? -
同样,如果您使用验证插件的
group函数来触发验证,您需要确保在页面加载期间不执行该调用。见fiddle。 -
您没有提供足够的代码来重现您的场景。你能用更多的代码更新这个问题吗,也许有小提琴的支持? (如果我想象你的代码可能类似于it works just fine:加载时没有错误消息。)
-
我建议你使用github.com/Knockout-Contrib/Knockout-Validation你不必重新发明轮子。
标签: javascript validation knockout.js