开发者

jquery validation plugin inside ajax loaded content [duplicate]

This question already has answers here: Jquery Validate with generated id's (2 answers) Closed 7 years ago.

I am using jquery validation plugin to validate an ajax submited form. the ajax call returns the html of the form if there was some error server side.

The problem is if this happens to ajax doens´t work anymore. I would need something like "live" jquery function that works with the jquery validation plugin.

here is my code:

$('#myform').validate({
            submitHandler: function(form) {
              formData = $(form).serialize();


              $.ajax({
                    type: "POST",
                    url: form.action,
                    data: formData ,
                    dataType: 'json',
                    success: function(response){
                        if(response.status == "success")
                            window.location.reload(true);
                        else
                           $('#myformdiv').html(response.html);

                    }开发者_如何学Python,
                    error: function(response){

                    }
                });

              return false;
            }
        });


Digging around in this topic on ajax loaded form validation for too long, the only solution the helped to solve my problem was this disscusion thread:

http://forums.asp.net/p/1651961/4391703.aspx?Re+Unobtrusive+validation+not+working+on+form+loaded+by+Ajax

Calling the following line after the new form is loaded. If the new form is in a jquery dialog call it after the dialog was opened.

$.validator.unobtrusive.parse("#myAjaxLoadedFormId");>


You need to re run validation scripts after include from inside html.

I had same problem. Form validations was working when I load it from server at page load but after ajax validation gone. So I add this code after embed the form to html and it's work.

$('#myform').validate({
        submitHandler: function(form) {
          formData = $(form).serialize();


          $.ajax({
            type: "POST",
            url: form.action,
            data: formData ,
            dataType: "html",
            cache: false,
            success: function (data) {
                $('#myformdiv').html(data);
                $.getScript("/Scripts/jquery.validate.unobtrusive.js");
            },
            error: function (xhr, status, error) {
                alert(xhr.responseText);
            }
          });

          return false;
        }
    });


you should consider reading this question and my answer to it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜