jQuery Validate Plugin - 字段生效后不立即删除错误消息

时间:2012-04-16 20:13:41

标签: jquery-validate

我正在尝试在下面的表单上使用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>

单击“验证”时,字段旁会显示验证错误消息。但是,一旦字段变得有效,错误消息就不会消失,就像在演示here中一样。有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:0)

嗯,对我有用。也许尝试最新的jquery.validate插件?我使用的版本是here