jQuery根据复选框状态进行验证 [英] jQuery validate depending on checkbox status

查看:103
本文介绍了jQuery根据复选框状态进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用jquery validate.js的表单中有三个字段:

I have three fields in a form which is using jquery validate.js:

<input type="text" id="StartDate" name="StartDate"><br />
<input type="text" id="EndDate" name="EndDate"><br />
<label for="AllDate"><input type="checkbox" name="AllDate" id="AllDate">All Dates</label>

我需要#StartDate才能提交,除非选中#AllDate.如果选中了#AllDate,则不需要#StartDate

I need #StartDate to be required on submit UNLESS #AllDate is checked. If #AllDate is checked, #StartDate is NOT required

尝试失败

rules: {
        StartDate: {
            required: {
                depends: function(element) {
                    return (! $("input[type='checkbox']:checked"))
                }
            }
        }
    }

我也没有创建自定义验证规则的运气.寻找一种更好的方法,一种可行的方法!帮助总是值得赞赏的.

I have had no luck creating a custom validation rule either. Looking for a better way, one that works! Help is always appreciated.

推荐答案

尝试一下:

return !$("#AllDate").is(':checked');

这篇关于jQuery根据复选框状态进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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