我正在这样创建一个动态元素:
strDivAttend += "<div class=\"row\" id=\"otherAttendee__" + i + "\"><div class=\"col-md-12\"><div class=\"col-md-3\"><div class=\"form-group\">";
strDivAttend += "<label class=\"control-label required\" for=\"newAttendees_" + i + "__AttendeeName\">Attendee Name</label>";
strDivAttend += "<input type=\"hidden\" name=\"newAttendees.Index\" value='" + i + "' /><input class=\"form-control text-box single-line\" data-val=\"true\" data-val-required=\"The Attendee Name field is required.\" id=\"newAttendees_" + i + "__AttendeeName\" name=\"newAttendees[" + i + "].AttendeeName\" type=\"text\" value=\"\" />";
strDivAttend += "<span class=\"field-validation-valid text-danger\" data-valmsg-for=\"newAttendees[" + i + "].AttendeeName\" data-valmsg-replace=\"true\"></span>";
strDivAttend += "</div></div>";
strDivAttend += "<div class=\"col-md-3\" style=\"margin-top: 35px;\">";
strDivAttend += "<a id=\"removeOtherAttend__" + i + "\" href=\"javascript:void(0)\" class=\"removeButtonAttend\" style=\"color:#aaa;\"><i class=\"fa fa-times-circle\"></i> remove</a>";
strDivAttend += "</div></div></div>";
$('#dvOtherAttendees').append(strDivAttend);
我遇到的问题是,即使动态创建的脚本中具有字段验证,也未强制执行。任何帮助都会有所帮助。
答案 0 :(得分:0)
客户端验证是在初始页面加载初始化,动态添加之后的任何元件将不被验证,除非你重新解析文档。
$('form').removeData('validator').removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse(document);