简体   繁体   English

客户端验证在jQuery UI对话框中不起作用

[英]Client Side validations not working in jQuery UI Dialog

I searched and found lot of answers, still i am not able to validate fields in my jQuery UI Dialog. 我搜索并找到了很多答案,但仍然无法验证jQuery UI对话框中的字段。 Please suggest appropriate ways for the same. 请提出同样的适当方法。

Parent View: 父视图:

<div id="dialog" title="Add New">
    <p>Div content</p>
</div>

<script type="text/javascript">$(function () {
        $('#btnAddNew').click(function () {
            $('#dialog').dialog
            ({
                autoOpen: true,
                modal: true,
                open: function (event, ui) {
                    $(this).load("@Url.Action("AddNewFormPart1")"); //Rendering Partial View
                }
            });
        });
    });
<script/>

Partial View: 部分视图:

@using (Html.BeginForm("SubmitFormPart1", "Home", FormMethod.Post, new { @id = "formPart1" }))
 {
    @Html.ValidationSummary()
    <div>......
    <input type="submit" value="Submit" id="btnSubmitFormPart1"/>
    </div>
 }

Validator should parse partial page after loading partial page. 加载部分页面后,验证程序应解析部分页面。 if you are using Unobtrusive Validation: 如果您使用的是非侵入式验证:

$(this).load('@Url.Action("AddNewFormPart1")', function(response, status, xhr){
    var form = $("#AddNewFormPart1");
    form.removeData('validator');
    form.removeData('unobtrusiveValidation');
    $.validator.unobtrusive.parse(form);
})

It is because your content are loaded after the plugin call! 这是因为您的内容是在插件调用之后加载的! You need to call the validation plugin after loading the partial view as follows(Notice the .done(function*({}) helper). 您需要按如下方式加载部分视图后调用验证插件(请注意.done(function*({}) helper)。

<script type="text/javascript">$(function () {
    $('#btnAddNew').click(function () {
        alert('Add New clicked!');
        $('#dialog').dialog
        ({
            autoOpen: true,
            modal: true,
            open: function (event, ui) {
                $(this).load("@Url.Action("AddNewFormPart1")").done(function(){

                    //CALL YOUR VALIDATION PLUGIN HERE
                    //$('#formPart1').validate({...});
                }); //Rendering Partial View
            }
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM