簡體   English   中英

選擇元素的敲除驗證出現在頁面加載中

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

我遇到的問題與此問題完全相同。 但是,公認的解決方案不能解決問題。

這是我的表單元素標記:

<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綁定)。 也許我錯過了一步... 上一個問題的答案含糊不清,幾乎沒有用。

還有其他人解決過這個問題嗎?

解決您的問題非常容易。 首次初始化viewModel時,需要調用此指令

self.Errors.showAllMessages(false)

這將禁用首次加載時的所有錯誤。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM