尝试使用jQuery Validate插件验证依赖于另一个选项的字段时出现错误 [英] Getting an error when trying to validate fields that depend on another option using jQuery Validate plug-in

查看:101
本文介绍了尝试使用jQuery Validate插件验证依赖于另一个选项的字段时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将jQuery Validate用于表单.我需要添加一些功能,因此,如果从选择/下拉菜单中选择了特定选项,则需要一个附加字段.

I'm using jQuery Validate for a form. I need to add some functionality so an additional field is required if a specific option is selected from a select/drop-down menu.

这是他们在jQuery网站上提供的示例,如果在复选框中选中了一个选项,则需要certrain字段(http://docs.jquery.com/Plugins/Validation/validate#options):

Here's the example they provide on the jQuery website to require a certrain field if an option is checked in a checkbox (http://docs.jquery.com/Plugins/Validation/validate#options):

$(".selector").validate({
   rules: {
     contact: {
       required: true,
       email: {
         depends: function(element) {
           return $("#contactform_email:checked")
         }
       }
     }
   }
})

如果选择了特定选项,它将在显示其他字段的地方工作.一旦选择菜单失去焦点,控制台中就会出现以下错误:

I have it working where it will display the additional fields if a particular option is selected. Once the select menu loses focus, the following error appears in the console:

未捕获的TypeError:无法调用方法 未定义的呼叫"

Uncaught TypeError: Cannot call method 'call' of undefined

我在JSFiddle上发布了一个示例.如果有人可以看看,我将不胜感激.我需要尽快开始工作!

I have an example posted on JSFiddle. If someone could take a look I would greatly appreciate it. I need to get this working ASAP!

http://jsfiddle.net/2EfPZ/

推荐答案

修复了小提琴(并简化了一点):

Fixed your fiddle (and simplified it a bit):

http://jsfiddle.net/2EfPZ/21/

由于以下原因:

ignore: ':hidden'

这篇关于尝试使用jQuery Validate插件验证依赖于另一个选项的字段时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆