简体   繁体   English

选择元素的敲除验证出现在页面加载中

[英]Knockout validation for select element appears on page load

I'm having the exact same problem as described in this question . 我遇到的问题与此问题完全相同。 But the accepted solution does not solve the problem. 但是,公认的解决方案不能解决问题。

Here's my form element markup: 这是我的表单元素标记:

<select
    class="form-control"
    data-bind="options: industries, optionsText: 'description', optionsValue: 'code', optionsCaption: 'Choose...', value: industry_code"
></select>

My data looks like this: 我的数据如下所示:

[
    {"code":"00","description":"Unclassified","parent":null},
    {"code":"01","description":"Amusement\/Arcade","parent":null},
    ...
];

And I'm trying to make this field required with the following code: 我正在尝试使用以下代码将此字段设为必填:

self.industry_code.extend({
    required: { message: "Please select an industry." }
});

It works perfectly for selecting, updating, etc. but when I load the form for the first time, the "Please select an industry." 它非常适合选择,更新等,但是当我第一次加载表单时,“请选择行业”。 message appears. 出现信息。 None of my other knockout validation code (for <input type='text'> elements) does this, so I'd like this one to behave the same way. 我的其他敲除验证代码(用于<input type='text'>元素)都没有做到这一点,所以我希望这一行为也具有相同的方式。

I did try the solution mentioned in the question cited above (in fact, I was already using the optionsCaption binding). 我确实尝试了上述问题中提到的解决方案(实际上,我已经在使用optionsCaption绑定)。 Maybe the I'm missing a step... the answer to the previous question is vague to the point of being almost useless. 也许我错过了一步... 上一个问题的答案含糊不清,几乎没有用。

Has anyone else solved this problem? 还有其他人解决过这个问题吗?

The solution for your problem is quite easy. 解决您的问题非常容易。 When you first initial your viewModel you need to call this instruction 首次初始化viewModel时,需要调用此指令

self.Errors.showAllMessages(false)

This will disable all the errors on first load. 这将禁用首次加载时的所有错误。 Hope this helps. 希望这可以帮助。

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

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