使用KnockoutJS和jQuery对话框jQuery的时候验证失败 [英] jQuery validation fails when using KnockoutJS and Jquery dialog

查看:109
本文介绍了使用KnockoutJS和jQuery对话框jQuery的时候验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在MVC3使用html.RenderAction呈现的一种形式。

I have a form which is rendered using html.RenderAction in MVC3.

此之外我有淘汰赛使用一个jQuery模板。该模型是正确呈现与默认的数据-VAL-必需的'属性的看法。

Outside of this I have a jquery template used with knockout. The model is rendered correctly into the view with the default 'data-val-required' attributes.

不过,我已经注意到,jQuery验证总是返回true。

However I've noticed that jQuery validation always returns true.

<div id="dlgAdd" data-bind="template: { name: 'editTemplate', data: selected }">
</div>
<script id="editTemplate" type="text/html">  
<div> 
@{
    Html.RenderAction("EditDialog");
}
</div>    
</script>

该EditDialog部分呈现以下输出像这样:

The EditDialog partial renders the following output like so:

 <form method="post" id="frmAddNew" action="/Project/AddNew"> 
    <div class="fields-inline">         
       <div class="editor-label">             
          <label for="Name">Name</label>         
       </div>         

       <div class="editor-field">
          <input data-val="true" data-val-required="The Name field is required." id="Name" name="ko_unique_41" value="" type="text">
          <span class="field-validation-valid" data-valmsg-for="Name" data-valmsg-replace="true"></span>
       </div>
    </div>
    <span id="validationmessage" class="field-validation-error"></span>   
  </form>

然而,当我打电话 $(#frmAddNew)。有效的()),它总是返回真。
我不知道,如果它的淘汰赛,jQuery的MVC或者是$ P $从返回false pventing验证。

However, when I call $("#frmAddNew").valid()), it always returns 'true'. I don't know if its knockout, jQuery or mvc which is preventing the validation from returning false.

推荐答案

尝试调用 $。validator.unobtrusive.parse(yourFormElement)来得到你的数据 - 属性相关分析验证

Try calling $.validator.unobtrusive.parse(yourFormElement) to get your data- attributes related to validation parsed.

您可以触发它,如:

<div id="dlgAdd" data-bind="template: { name: 'editTemplate', data: selected, afterRender: hookUpValidation  }">
</div>

那么,hookUpValidation看起来像:

then, hookUpValidation would look like:

hookUpValidation: function(nodes) {
    $.validator.unobtrusive.parse(nodes[0]);
}

这篇关于使用KnockoutJS和jQuery对话框jQuery的时候验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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