在模型的驗證,財產ItemDescription我已經設置屬性[必需] 根據我的UI選擇,我做這個領域的看法有時可見,有時候不可見的。刪除看不見的表單字段
如何使不顯眼的驗證忽略[必需的]屬性當此字段不可見時。
這是我試過到目前爲止:
$(document).ready(function() {
//doesn't seem to do anything. The field is still validated
$("form").validate().settings.ignore = "#ItemDescription";
//throws an error
$("form").data("validation").settings.ignore = "#ItemDescription";
//doesn't seem to do anything. The field is still validated
$.validator.setDefaults({
ignore: "#ItemDescription"
});
//doesn't seem to do anything. The field is still validated
$("#ItemDescription").removeAttr("data-val");
$("#ItemDescription").removeAttr("data-val-required");
});
我的猜測是,我來不及做這些調用。 我使用jQuery 1.9.0和1.9.0 jquery.validate
感謝
你是如何隱藏價值的?如果您最初使用'@ Html.HiddenFor()'渲染它,則不會執行驗證。但是,如果你使用jQuery來隱藏它,那麼它太晚了($ .validator已經解析了表單,你需要重新解析它) – 2014-12-03 22:14:27