简体   繁体   English

jQuery验证依赖项表达式

[英]jQuery Validate dependency-expression

I'm trying to set up a jQuery Validate dependency-expression so that users need to either use the text field OR upload a file, but this code is not working: both fields are always required. 我正在尝试设置jQuery Validate 依赖项表达式,以便用户需要使用文本字段或上载文件,但是此代码不起作用:始终都需要两个字段。 I'm guessing <input type="file"> isn't supported by #id:empty , but what can I use in its place? 我猜测#id:empty不支持<input type="file"> ,但是我可以用它代替什么?

Here's a jsFiddle. 这是一个jsFiddle。

Javascript: 使用Javascript:

jQuery(document).ready(function() {
    jQuery("#form1iienom").validate({
        errorClass:"errorlabels",
        rules: {
            form1apptext: {
                required: "#form1upload:empty"
            },
            form1upload: {
                required: "#form1apptext:empty",
                extension: "jpg|jpeg|pdf|png|doc|docx"
            },
        },
        messages: {
            form1apptext: "You must either enter a reason for your nomination or upload a file.",
            form1upload: {
                required: "You must either enter a reason for your nomination or upload a file.",
            },
        }
    });
});

HTML: HTML:

<form name="form1iienom" id="form1iienom" method="post" action="/applications/iie/review" enctype="multipart/form-data">
<textarea name="form1apptext" id="form1apptext"></textarea><br />
<br />
<em>(allowed file types are jpg, jpeg, pdf, png, doc, and docx; maximum file size is 3 MB)</em>
<input type="hidden" name="MAX_FILE_SIZE" value="3145728" />
<input type="file" name="form1upload" id="form1upload" /><br />
<input type="submit" value="Submit" />
</form>

As per documentation , the proper selector is :blank , not :empty . 根据文档 ,正确的选择器是:blank ,不是:empty

Also see the example, as per documentation , using the depends sub-rule... 请参见根据文档的示例,使用depends子规则...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM