1

我有一个使用此插件验证的表单:http: //bassistance.de/jquery-plugins/jquery-plugin-validation/

我决定使用.toggle().

这是表格:

<form id="identification" action="Test.php" method="post">
    <fieldset>
        <legend>Identification</legend>
        <p>
            <label for="firstName">First name</label>
            <input id="firstName" type="text" name="firstName" class="required"/>
        </p>
        <p>
            <label for="lastName">Last name</label>
            <input id="lastName" type="text" name="lastName" class="required"/>
        </p>
        <p>
            <label for="email">E-mail address</label>
            <input id="email" type="text" name="email" class="required email"/>
        </p>
        <div class="hiddenFields">
            <p>
                <label for="zip">Zip code</label>
                <input id="zip" type="text" name="zip" class="required zip"/>
            </p>
            <p>
                <label for="city">City</label>
                <input id="city" type="text" name="city" class="required"/>
            </p>        
        </div>
        <p class="showFields"><a class="toggle" href="#">Click here</a> for more info</p>
    </fieldset> 
    <p class="submit"><input type="submit" value="Submit"/></p>
</form>

.hiddenFieldsdiv 设置为在我的display: none;CSS 中。

这是验证脚本:

$(document).ready(function(){
    $("#identification").validate({
        rules: {
                "zip": { digits:true, minlength:5, maxlength:5 }
        }
    });

    jQuery.extend(jQuery.validator.messages, {
        required: "This field is needed.",
        email: "Please enter a valid e-mail address.",
        digits,minlength,maxlength: "Please enter a valid zip code.",
        minlength: "Please enter a valid zip code.",
        maxlength: "Please enter a valid zip code."
    });
});

这是切换脚本:

$(document).ready(function(){
    $("a.toggle").click(function(e){
        e.preventDefault();
        $(".hiddenFields").toggle("fast");
    });
});

.hiddenFields现在,没有div 和没有 div一切正常,.toggle()但是当我尝试添加它时,验证不再起作用,我真的不明白为什么。

当字段被隐藏和不被隐藏时,我如何设法验证此表单?它目前在这两种情况下都不起作用。

谢谢你的帮助。

4

1 回答 1

1

这是在 jsFiddle 上

您有一个流氓digits,minlength,导致语法错误:

jQuery.extend(jQuery.validator.messages, {
   required: "This field is needed.",
   email: "Please enter a valid e-mail address.",
   digits,minlength,maxlength: "Please enter a valid zip code.",
   minlength: "Please enter a valid zip code.",
   maxlength: "Please enter a valid zip code."
});

你也进了maxlength两次。

将其更改为此将修复它:

jQuery.extend(jQuery.validator.messages, {
   required: "This field is needed.",
   email: "Please enter a valid e-mail address.",
   minlength: "Please enter a valid zip code.",
   maxlength: "Please enter a valid zip code."
});
于 2012-05-30T10:17:20.130 回答