jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何通过jquery在show / hide上启用/禁用不显眼的验证?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
更新:
我要问的是,是否可以删除验证规则,这些验证规则仅在客户端的viewmodel中的字段上的Attributes中推断出来.

当从下拉列表中进行选择时,我正在切换某些字段的显示.我还需要切换验证.

viewmodel.cs:

public int ddlTypeID { get; set; }
public Dictionary<int,string> ddlTypes { get; set; }

[required(ErrorMessageResourceName = "msgrequired",ErrorMessageResourceType = typeof(Resources.Globals))]
public DateTime firstDate {get; set;}

[required(ErrorMessageResourceName = "msgrequired",ErrorMessageResourceType = typeof(Resources.Globals))]
public DateTime otherDate {get; set;}

Create.cshtml:

<script type="text/javascript">
    $(document).ready(function () {
       $('.optional').hide();
       $('#ddlTypeID').change(function () {
            var id = $(this).val();
            if (id == 1) {
                $('.optional').show();
            } else {
                $('.optional').hide();
            }
        });
    });
</script>

@using (Html.BeginForm())
{
    <div class="editor-field">
        @Html.DropDownListFor(x=>x.ddlTypeID,new SelectList(Model.ddlTypes,"Key","Value",Model.ddlTypeID),Resources.Globals.msgType)
        @Html.ValidationMessageFor(model => model.ddlTypeID)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.firstDate )
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.firstDate )
        @Html.ValidationMessageFor(model => model.firstDate )
    </div>

    <div class="editor-label optional">
        @Html.LabelFor(model => model.otherDate )
    </div>
    <div class="editor-field optional">
        @Html.EditorFor(model => model.otherDate )
        @Html.ValidationMessageFor(model => model.otherDate )
    </div>

    <input type="submit" value="Save" />
}

这可以隐藏/显示otherDate的字段 – 但是如何在同一时间切换验证?或者有更好的方法吗?

解决方法

它的确有效.
但是,当您在表单上隐藏元素时,需要调用代码.
正确的代码是:

function IgnoreValidationForHiddenElements() {

  $.validator = $("#formid").validate(
               {
                   ignore: ":hidden"
               });
}

或者当你穿上

$(document).ready(function () {
//Your Code
}

表单加载时需要此选项时.

大佬总结

以上是大佬教程为你收集整理的如何通过jquery在show / hide上启用/禁用不显眼的验证?全部内容,希望文章能够帮你解决如何通过jquery在show / hide上启用/禁用不显眼的验证?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: