我怎样才能使通过jQuery上显示/隐藏/禁用不引人注目的验证? [英] How can I enable/disable unobtrusive validation on show/hide via jquery?

查看:101
本文介绍了我怎样才能使通过jQuery上显示/隐藏/禁用不引人注目的验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:
什么我问的是它是否可以删除那些从属性上推断字段我的视图模型验证规则,在客户端只。

Update: What I'm asking is whether it's possible to remove validation rules which are inferred from Attributes on fields in my viewmodel, on the client side ONLY.

-

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

I am toggling the display of certain fields when a choice is made from a dropdown list. I need to also toggle the validation.

ViewModel.cs:

ViewModel.cs:

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

[Required(ErrorMessageResourceName = "msgRequired", ErrorMessageResourceType = typeof(Resources.Globals))]
pubic DateTime firstDate {get; set;}

[Required(ErrorMessageResourceName = "msgRequired", ErrorMessageResourceType = typeof(Resources.Globals))]
pubic DateTime otherDate {get; set;}

Create.cshtml:

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领域 - 但我如何可以切换在同一时间的验证?还是有更好的方式来做到这一点?

This works to hide/show the fields for otherDate - but how can I toggle the validation at the same time? Or is there a better way to do this?

推荐答案

这工作真。
但是,你需要调用此code当你隐藏在表单元素。
右code是:

It Works true. But you need to call this code when you have hidden elements on form. right code is :

function IgnoreValidationForHiddenElements() {

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

,或当你穿上

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

当你需要这个选项时窗体的Load。

when you need this option when form Load.

这篇关于我怎样才能使通过jQuery上显示/隐藏/禁用不引人注目的验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆