unobtrusive validation not working with dynamic content

后端 未结 7 1710
一生所求
一生所求 2020-11-28 02:07

I\'m having problems trying to get the unobtrusive jquery validation to work with a partial view that is loaded dynamically through an AJAX call.

I\'ve been spending

7条回答
  •  一向
    一向 (楼主)
    2020-11-28 02:47

    I got struck in the same problem and nothing worked except this:

    $(document).ready(function () { 
        rebindvalidators();
    });
    
    function rebindvalidators() {
        var $form = $("#id-of-form");
        $form.unbind();
        $form.data("validator", null);
        $.validator.unobtrusive.parse($form);
        $form.validate($form.data("unobtrusiveValidation").options);
    }
    

    and add

    // Check if the form is valid
    var $form = $(this.form);
    if (!$form.valid())
        return;
    

    where you are trying to save the form.

    I was saving the form through Ajax call.

    Hope this will help someone.

提交回复
热议问题