jQuery .validate 在聚焦时抛出异常。说 "check 'depend' method"
jQuery .validate throws exception when focus out. Says "check 'depend' method"
我正在使用 jQuery .validate 插件进行 jquery 表单验证。只有在选中单选按钮(在本例中为 "Hey Yo")时才需要一个文本字段(名称:"yo_text")。
现在 jQuery 可以告诉 yo_text 取决于 "Hey Yo" 被选中,但是每次 yo_text 模糊并且不为空时都会出现错误。如果之前的检查表明它无效(即 yo_text 为空)
,则文本字段中的错误消息也不会消失
错误:
Uncaught TypeError: Cannot read property 'call' of undefined. Exception occurred when checking element , check the 'depends' method.
我只是简单地修改了代码形式.validate的示例(见下文),但现在出现了神秘的错误。我不知道问题出在哪里。
所以标记:
<form>
<div class="radio">
<label>
<input type="radio" name="selection" value="yo"> Hey Yo
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="selection" value="ya"> Hey Ya
</label>
</div>
<input class="form-control" type="text" name="yo_text" placeholder="Enter if yo">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
和脚本:
var form = $('form');
function isYo() {
return form.find('input[type="radio"][name="selection"][value="yo"]')
}
form.validate({
rules: {
selection: 'required'
, yo_text: {
required: true,
depends: isYo
}
}
});
以及我在 .validate's documentation 中修改的示例:
$("#myform").validate({
rules: {
contact: {
required: true,
email: {
depends: function(element) {
return $("#contactform_email").is(":checked");
}
}
}
}
});
还有一个 JSFiddle 供参考:
https://jsfiddle.net/vzj1ckbx/
提前感谢您的帮助!
你的函数 isYo
有问题,我认为它应该 return 一个布尔值,但不是。在您的情况下,它将 return 元素。还对验证进行了一些修改。
https://jsfiddle.net/vzj1ckbx/4/
var form = $('form');
function isYo() {
return $('input[type="radio"][name="selection"][value="yo"]').is(':checked');
}
form.validate({
rules: {
"yo_text": {
required: {
param:true,
depends: isYo
}
}
},
submitHandler: function () {
alert('Ho hey')
event.preventDefault();
return false;
}
});
我正在使用 jQuery .validate 插件进行 jquery 表单验证。只有在选中单选按钮(在本例中为 "Hey Yo")时才需要一个文本字段(名称:"yo_text")。
现在 jQuery 可以告诉 yo_text 取决于 "Hey Yo" 被选中,但是每次 yo_text 模糊并且不为空时都会出现错误。如果之前的检查表明它无效(即 yo_text 为空)
,则文本字段中的错误消息也不会消失错误:
Uncaught TypeError: Cannot read property 'call' of undefined. Exception occurred when checking element , check the 'depends' method.
我只是简单地修改了代码形式.validate的示例(见下文),但现在出现了神秘的错误。我不知道问题出在哪里。
所以标记:
<form>
<div class="radio">
<label>
<input type="radio" name="selection" value="yo"> Hey Yo
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="selection" value="ya"> Hey Ya
</label>
</div>
<input class="form-control" type="text" name="yo_text" placeholder="Enter if yo">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
和脚本:
var form = $('form');
function isYo() {
return form.find('input[type="radio"][name="selection"][value="yo"]')
}
form.validate({
rules: {
selection: 'required'
, yo_text: {
required: true,
depends: isYo
}
}
});
以及我在 .validate's documentation 中修改的示例:
$("#myform").validate({
rules: {
contact: {
required: true,
email: {
depends: function(element) {
return $("#contactform_email").is(":checked");
}
}
}
}
});
还有一个 JSFiddle 供参考:
https://jsfiddle.net/vzj1ckbx/
提前感谢您的帮助!
你的函数 isYo
有问题,我认为它应该 return 一个布尔值,但不是。在您的情况下,它将 return 元素。还对验证进行了一些修改。
https://jsfiddle.net/vzj1ckbx/4/
var form = $('form');
function isYo() {
return $('input[type="radio"][name="selection"][value="yo"]').is(':checked');
}
form.validate({
rules: {
"yo_text": {
required: {
param:true,
depends: isYo
}
}
},
submitHandler: function () {
alert('Ho hey')
event.preventDefault();
return false;
}
});