0

我正在尝试在如下表单上使用 jQuery Validate 插件:

JS:

<script src="jquery-1.7.1.js" type="text/javascript"></script>
<script src="jquery.validate.js" type="text/javascript"></script>
<script>
function ValidateFields() {

    $("#createProgramForm").validate({
        rules: {
            firstname: { required: true, minlength: 2 },
            lastname: { required: true }
        },
        messages: {
            firstname: { minlength: "Minimum 2 characters." },
            lastname: { required: "Please enter a last name." }
        }
    });


    if ($("#createProgramForm").valid()) {
        alert("Form is valid");
    }

}
</script>

HTML:

<body>
<div id="page">
    <form id="createProgramForm">
        <p>
            <label for="firstname">
                First Name</label>
            <input name="firstname" id="firstname" type="text" />
        </p>
        <p>
            <label for="lastname">First Name</label>
            <input name="lastname" id="lastname" type="text" />
        </p>
        <p>
            <button onclick="ValidateFields();">Validate</button>
        </p>
    </form>
</div>
</body>

当我单击验证时,验证错误消息会显示在字段旁边。但是,一旦字段变为有效,就像这里的演示一样,错误消息就不会消失。有谁知道为什么会这样?

4

1 回答 1

0

嗯,为我工作。也许尝试最新的 jquery.validate 插件?我用的版本在这里

于 2012-04-16T20:32:30.273 回答