简体   繁体   English

条件必填字段验证不起作用

[英]Conditional Required Fields validation is not working

I've got a new enigma for you to solve, because I can't manage to get this working. 我有一个新的谜题需要解决,因为我无法设法解决这个问题。

Imagine yourself working for an online marketing company with a proprietary platform to host and manage campaigns. 想象一下,您在一家拥有专有平台来托管和管理广告活动的在线营销公司中工作。 And then imagine only being able to validate required fields that 'just are there'. 然后想象只有能够验证“就在那儿”的必填字段。 And at last; 最后 imagine a client that needs form fields to be required after a certain radio button is checked. 假设某个客户在选中某个单选按钮后需要表单字段。 Right? 对? Right! 对! Well.... no... not right. 好吧....不...不对。

Here is the thing. 这是东西。 That same radio button triggers a jQuery script that eases in the 5 div 's with form fields that are hidden display: block . 相同的单选按钮会触发一个jQuery脚本,该脚本可简化5 div的隐藏字段display: block I have the jQuery.validation.js plugin at the ready. 我已经准备好了jQuery.validation.js插件。

I insert this piece of code to the form fields that are required if it meets the condition that the depending field is checked: 如果满足检查依赖字段的条件,我会将这段代码插入所需的表单字段中:

class="textInput1 validate[condRequired[radiobuttonX]]" 

textInput1 is a CSS class that styles an input field. textInput1是为输入字段设置样式的CSS类。

As a submitbutton I use an image which uses this code to submit: input type="image" 作为一个submitbutton我使用一个使用此代码提交的图像: input type="image"

onclick="document.forms['formName'].submit();"

I have got: 我有:

$(document).ready(function() {

in place and in my head I have the link to the validation script: 在我的脑海中,我拥有验证脚本的链接:

<script src="../path/to/jquery.validationEngine.js"></script>

I use the: Inline Form Validation Engine 2.6.2, jQuery plugin 我使用: 内联表单验证引擎2.6.2,jQuery插件

How do I make form field Y required when radiobuttonX is ticked? 选中radiobuttonX时,如何使表单字段Y为必填项? I definitely need syntax as well, because I'm a dumbs ;-) 我当然也需要语法,因为我很笨;-)

If you can switch fromjQuery Validation Engine to jQuery-Validate, this would be the solution: 如果您可以从jQuery Validation Engine切换到jQuery-Validate,这将是解决方案:

$("#form").validate({
    rules: {
        fieldY: {
            required: {
                depends: function() {
                    return $("#radioButtonX").is(":checked");
                }
            }
        }
    }
});

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

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