jQuery非侵入式自定义适配器和jsFiddle中的方法 [英] jQuery unobtrusive custom adapter and method in jsFiddle
问题描述
我无法使此jsFiddle工作,但它可以在浏览器中工作: http://jsfiddle.net/vtortola/jYq2X/
I cannot make this jsFiddle work but it works in the browser: http://jsfiddle.net/vtortola/jYq2X/
我正在尝试添加新的自定义规则来比较两个字段.自定义适配器有效,正在被调用并设置选项.但是永远不会调用自定义方法.
I am trying to add a new custom rule to compare two fields. The custom adapter works, it is being called and setting the options. But the custom method is never called.
我正在DOM上执行此JS:
I am executing this JS on DOM ready:
$.validator.addMethod("customequal-method", function (val, el, p) {
var $other = $(el).closest('form').find('input[name=' + p.other + ']');
return $other.length && $other.val() == val;
});
$.validator.unobtrusive.adapters.add("customequal", ["other"],
function (options) {
options.rules["customequal-method"] = options.params;
options.messages["customequal-method"] = options.message;
});
$(function(){
$.validator.unobtrusive.parse($('#myform'));
$('[type=button]').click(function(e){e.preventDefault(); $('form').valid();});
$('input[type=text]').blur();
})
这些是HTML中的字段:
These are the fields in HTML:
<input type="text" name="StartDate2" id="StartDate2" value="2"
data-val="true" data-val-customequal="xx xxx" data-val-customequal-other="EndDate2"/>
<input type="text" name="EndDate2" id="EndDate2" value="3"
data-val="true" data-val-customequal="xx xx" data-val-customequal-other="StartDate2"/>
我一直在尝试不同的方法,但似乎无济于事.
I have been trying different things but nothing seems to work.
有什么主意吗?
推荐答案
您的小提琴无法正常工作,因为:
-
所有代码都可以在DOM中运行,因此您要在名为
unobtrusive.parse(document)
的unobtrusive.validator插件之后添加自定义unobtrusive.adapters.add
,该插件会在没有自定义验证器的情况下注册所有输入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 calledunobtrusive.parse(document)
which registers all the inputs without your custom validator如果您多次调用 .validate(),则它仅首次注册规则并且不会在后续调用中覆盖它们.因此,尽管您这次在加载了自定义适配器的DOM中再次调用了
unobtrusive.parse
,它仍然没有任何作用.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.在DOM加载事件之前注册您的自定义适配器,您可以通过更改小提琴来使用
Register your custom adapters before the DOM loaded event, you can do this with changing your fiddle to use
"No wrap - in <head>"
演示 JSFiddle .
使用
$('#myform').data('validator', null)
删除已添加的验证器对象,然后手动调用unobtrusive.parse
:Remove the already added validator object with using
$('#myform').data('validator', null)
before callingunobtrusive.parse
manually:$(function () { $('#myform').data('validator', null); $.validator.unobtrusive.parse($('#myform')); $('[type=button]').click(function (e) { e.preventDefault(); $('form').valid(); }); $('input[type=text]').blur(); })
演示 JSFiddle .
这篇关于jQuery非侵入式自定义适配器和jsFiddle中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!