jQuery unobtrusive custom adapter and method in jsFiddle

早过忘川 提交于 2019-12-06 03:37:58

Your fiddle is not working because:

  • all your code runs in the DOM ready so you are adding your custom unobtrusive.adapters.add after the unobtrusive.validator plugin called unobtrusive.parse(document) which registers all the inputs without your custom validator

  • if you call .validate() multiple times it only registers the rules for the first time and does not override them on subsequent calls. So although you've called unobtrusive.parse again in the DOM loaded this time with the custom adapter added it still won't have any effect.

So you have two ways to fix it:

Register your custom adapters before the DOM loaded event, you can do this with changing your fiddle to use

"No wrap - in <head>"

Demo JSFiddle.

Or

Remove the already added validator object with using $('#myform').data('validator', null) before calling unobtrusive.parse manually:

$(function () {
    $('#myform').data('validator', null);

    $.validator.unobtrusive.parse($('#myform'));
    $('[type=button]').click(function (e) {
        e.preventDefault();
        $('form').valid();
    });

    $('input[type=text]').blur();
})

Demo JSFiddle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!