How to check if a <div> is valid with unobtrusive javascript

流过昼夜 提交于 2019-12-12 03:56:58

问题


I have a form which loads a div on client side. I have hard coded textbox controls with all validation attributes, similar to what it renders when loaded from server. Inside the div there is submit button, but when I click on submit all validations messages on the form are displayed. I just need only the div elements validation messages to be shown. Telerik Grid control in ajax mode, does similar thing, i.e., appends textboxes with hardcoded validation attributes on client side, but it manages to fire validations only for the grid not entire form. I think I am missing something here.

$('#div').valid() --> doesn't work
$('#form').valid() --> works

回答1:


You need to parse the validation rules of the containing form once you add the div to the DOM:

var form = $('#div').closest('form');
form.removeData('validator');
form.removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(form);

and here's a live demo.



来源:https://stackoverflow.com/questions/12516245/how-to-check-if-a-div-is-valid-with-unobtrusive-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!