为什么自定义验证器方法不触发?
why custom validator method not firing?
我创建了一个自定义验证,我想用它来比较 2 个 select 框,但是附加方法没有触发不知道如何解决?
这是 jsfiddle:http://jsfiddle.net/ughCm/97/
在更改 select 值时,它不会触发 'comaremodelyear' 方法,甚至不会触发 .valid().
这是代码
$.validator.addMethod("comparemodalyear", function(value, element, param) {
alert('h');
return true;
}, "From year must be greater!");
$(document).ready(function() {
$('#savebtn').click(function() {
var test = $("#experiment").valid();
});
});
html代码:
<form id="experiment" action="/" method="post">
<input type="text" required name="fine" />
<select comparemodalyear name="testingA">
<option value="1">Val1</option>
<option value="2">Val2</option>
</select>
<button id="savebtn" type="button">Save</button>
</form>
您需要将其添加为 class 而不是属性
<select class="comparemodalyear" name="testingA">
<option value="1">Val1</option>
<option value="2">Val2</option>
</select>
然后
$.validator.addClassRules('comparemodalyear', {
comparemodalyear: true
});
演示:Fiddle
如果您不想将自定义值传递给规则,则不需要 addClassRules()
,因为 addMethod()
会根据以下条件对 addClassRules()
进行内部调用method.length < 3
条件,其中 method.length
是 count of arguments 验证方法
$.validator.addMethod("comparemodalyear", function (value, element) {
alert('h');
return true;
}, "From year must be greater!");
演示:Fiddle
我创建了一个自定义验证,我想用它来比较 2 个 select 框,但是附加方法没有触发不知道如何解决? 这是 jsfiddle:http://jsfiddle.net/ughCm/97/ 在更改 select 值时,它不会触发 'comaremodelyear' 方法,甚至不会触发 .valid().
这是代码
$.validator.addMethod("comparemodalyear", function(value, element, param) {
alert('h');
return true;
}, "From year must be greater!");
$(document).ready(function() {
$('#savebtn').click(function() {
var test = $("#experiment").valid();
});
});
html代码:
<form id="experiment" action="/" method="post">
<input type="text" required name="fine" />
<select comparemodalyear name="testingA">
<option value="1">Val1</option>
<option value="2">Val2</option>
</select>
<button id="savebtn" type="button">Save</button>
</form>
您需要将其添加为 class 而不是属性
<select class="comparemodalyear" name="testingA">
<option value="1">Val1</option>
<option value="2">Val2</option>
</select>
然后
$.validator.addClassRules('comparemodalyear', {
comparemodalyear: true
});
演示:Fiddle
如果您不想将自定义值传递给规则,则不需要 addClassRules()
,因为 addMethod()
会根据以下条件对 addClassRules()
进行内部调用method.length < 3
条件,其中 method.length
是 count of arguments 验证方法
$.validator.addMethod("comparemodalyear", function (value, element) {
alert('h');
return true;
}, "From year must be greater!");
演示:Fiddle