jQuery验证插件有多种形式

时间:2013-10-22 15:09:20

标签: jquery forms jquery-validate

我正在尝试为一系列表单添加一些功能来验证两个复选框。我正在使用jQuery验证插件,因为我的生活不能让它工作。在提交时,我只是在提交时收到403禁止消息。在我的实际网站上,它看起来似乎从未执行过。

HTML - 这些表单每页多个且相同。 Stackoverflow只允许我显示一个

<form method="post" id="offer" name="offer" action="#" class="offer">
    <input type="text" name="original_description">
    <input type="text" name="description">
    <input type="text" name="added_by">
    <input type="submit" id="add" value="add" class="submit">
</form>

JS

$(".offer").each(function () {
    $(this).validate({
        submitHandler: function (form) {
            var x = form.find('input[name=description]').val();
            var y = form.find('input[name=original_description]').val();
            var z = similar_text(x, y, 1);
            alert("Description: " + x + " original_description: " + y + " z: " + z)
            if (z > 50 && !isNaN(z)) {
                alert("Description too similar to original. Please make it less similar");
                return false;
            }
            form.submit();
        },
        rules: {
            added_by: {
                required: true
            }
        },
        messages: {
            added_by: {
                required: 'Please select your name'
            }
        }
    });
});

1 个答案:

答案 0 :(得分:2)

如评论中所述,您应该在rules:之前添加','并在submitHandler中操作jquery表单对象,如下所示:

$(".offer").each(function () {
    $(this).validate({
        submitHandler: function (form) {
            var x = $(form).find('input[name=description]').val();
            var y = $(form).find('input[name=original_description]').val();
            var z = similar_text(x, y, 1);
            alert("Description: " + x + " original_description: " + y + " z: " + z)
            if (z > 50 && !isNaN(z)) {
                alert("Description too similar to original. Please make it less similar");
                return false;
            }
            form.submit();
        },
        rules: {
            added_by: {
                required: true
            }
        },
        messages: {
            added_by: {
                required: 'Please select your name'
            }
        }
    });
});