jQuery验证插件多种形式 [英] jQuery validation plugin multiple forms

查看:70
本文介绍了jQuery验证插件多种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向一系列表单添加一些功能以验证两个复选框.我正在使用jQuery验证插件,并且终生无法正常工作.摆弄小提琴时,我刚收到一条403禁止消息.在我的实际网站上,它似乎从未执行过.

I'm trying to add some functionality to a series of forms to validate two checkboxes. I'm using the jQuery validation plugin and for the life of me can't get it working. In fiddle I just get a 403 forbidden message when I submit it. On my actual site it looks like its never even executed.

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

HTML - These forms are multiple and identical per page. Stackoverflow only lets me show one though

<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'
            }
        }
    });
});

推荐答案

如注释中所述,您应该在rules:之前添加一个',',并在您的SubmitHandler中操纵一个jquery表单对象,如下所示:

As stated in the comments, you should add a ',' before rules:, and manipulate a jquery form object in your submitHandler, like this :

$(".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'
            }
        }
    });
});

这篇关于jQuery验证插件多种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆