【问题标题】:jquery unobtrusive validation non working for dynamically loaded content in formjquery不显眼的验证不适用于表单中动态加载的内容
【发布时间】:2012-07-06 09:01:41
【问题描述】:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Microinvest.net.Models.ViewModels.SupportRusViewModel>" %>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("HelpRequest", "Form", FormMethod.Post, new {id ="formTrim" }))
   { %>
    <tr>
        <td>
            <label for="suggestion_suggestionMessage">
                <%= Resources.Dict.strSuggestionDescription%></label>
        </td>
        <td>
            <%= Html.TextAreaFor(x => x.suggestion.suggestionMessage, new { @class = "technicalQuestionTextArea" })%><span
                class="required">
                <br />
                <%= Html.ValidationMessageFor(m => m.suggestion.suggestionMessage)%></span>
        </td>
    </tr>
    <tr>
        <td>
            <label for="suggestion_systemDescription">
                <%= Resources.Dict.strSuggestionSystemDescription%></label>
        </td>
        <td>
            <%= Html.TextAreaFor(x => x.suggestion.systemDescription, new { @class = "technicalQuestionTextArea" })%><span
                class="required">
                <br />
                <%= Html.ValidationMessageFor(m => m.suggestion.systemDescription)%></span>
        </td>
    </tr>
    <% } %>

我正在该视图中加载该部分视图:

<% Html.EnableClientValidation(); %>
            <% using (Html.BeginForm("HelpRequest", "Form"))
               { %>
            <%= Html.ValidationSummary(true) %>
            <center>
                <fieldset>
                    <table cellpadding="3">
                        <tr>
                            <td valign="middle" align="right">
                                <label for="names">
                                    <%= Resources.Dict.strNames%></label>:<span class="required"></span>
                            </td>
                            <td align="left" style="width: 300px;">
                                <%= Html.TextBoxFor(m => m.names)%><span class="required"><br />
                                    <%= Html.ValidationMessageFor(m => m.names)%></span>
                            </td>
                        </tr>
                        <tr>
                            <td valign="middle" align="right">
                                <label for="city">
                                    <%= Resources.Dict.strOrganisation%></label>:<span class="required"></span>
                            </td>
                            <td align="left" style="width: 300px;">
                                <%= Html.TextBoxFor(m => m.city)%>
                            </td>
                        </tr>
                        <tr>
                            <td valign="middle" align="right">
                                <label for="telephone">
                                    <%= Resources.Dict.phoneNumber%></label>:<span class="required"></span>
                            </td>
                            <td align="left" style="width: 300px;">
                                <%= Html.TextBoxFor(m => m.telephone)%><span class="required"><br />
                                    <%= Html.ValidationMessageFor(m => m.telephone) %></span>
                            </td>
                        </tr>
                        <tr>
                            <td valign="middle" align="right">
                                <label for="email">
                                    <%= Resources.Dict.strEnterYourEmail%></label>:<span class="required"></span>
                            </td>
                            <td align="left" style="width: 300px;">
                                <%= Html.TextBoxFor(m => m.email)%><span class="required"><br />
                                    <%= Html.ValidationMessageFor(m => m.email) %></span>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <%= Resources.Dict.strQuestionType %>:
                            </td>
                            <td>
                                <%=Html.RadioButtonFor(m => m.questionType, "techinicalQuestion", new {@checked = "checked", @class = "questionType" })%>
                                <%= Resources.Dict.strTechnicalQuestion%>
                                <%=Html.RadioButtonFor(m => m.questionType, "suggestion", new { @class = "questionType" })%>
                                <%= Resources.Dict.strSuggestion %>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="2">
                                <div id="typeOfQuestion">
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">
                                <input id="register_button" type="submit" value="<%= Resources.Dict.strSend %>" />
                            </td>
                        </tr>
                    </table>
                </fieldset>
                <br />
            </center>
            <% } %>

我正在使用该 javascript 来完成这项工作:

    function pageLoad() {
        $("#typeOfQuestion").load("/Form/TechnicalQuestionPartial");
        $(".questionType").click(function () {
            var UrlPass;
            if ($(this).val() === "techinicalQuestion") {
                UrlPass = "/Form/TechnicalQuestionPartial";
                //            $('#typeOfQuestion').load("/Form/TechnicalQuestionPartial");
            } else if ($(this).val() === "suggestion") {
                UrlPass = "/Form/SuggestionPartial";
                //            $('#typeOfQuestion').load("/Form/Suggestion");
            }
            $.ajax({
                url: UrlPass,
                success: function (data) {
                    $('#typeOfQuestion').html(data);
                    $('#typeOfQuestion').html($("#formTrim").html());
                    validateAjaxForm();
                }
            });
        });


    function validateAjaxForm() {

        $('#form0').removeData('validator');
        $('#form0').removeData('unobtrusiveValidation');
        $.validator.validate('#form0');
        $('#form0').validate().form();
//        $("form").removeData("validator");
//        $("form").removeData("unobtrusiveValidation");
//        $.validator.unobtrusive.parse("form");
//         $('#form0').validate();
    }
};

form0 是呈现的非局部视图中表单的 id 问题是

$('#form0').removeData('validator');
        $('#form0').removeData('unobtrusiveValidation');
        $.validator.validate('#form0');
        $('#form0').validate().form(); 

没有开启动态输入内容的验证

【问题讨论】:

  • 你不能为此使用 HTML.Ajaxform 吗?
  • 你是什么意思我首先加载 1 个视图,然后当客户端单击单选按钮 $(".questionType").click(function () 我正在加载 2 个部分视图中的 1 个,我也需要验证动态加载的局部视图中的字段。如果我使用 html.beginForm 或 html.ajaxForm,如果在加载后使用 jquery 删除表单会有什么不同

标签: jquery validation unobtrusive-validation


【解决方案1】:

您可以尝试调用:

$.validator.unobtrusive.parse($("form"));

这应该有望启用对动态添加的内容的验证。

【讨论】:

  • $.validator.unobtrusive.parse($("form"));它对我不起作用
猜你喜欢
  • 1970-01-01
  • 2015-04-05
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多