使用多个 'div' 的 jQuery 验证插件

发布于 2024-12-02 03:45:45 字数 1405 浏览 1 评论 0原文

我正在使用一个很棒的 jQuery 验证插件。它在单个表单中运行良好,但如果我在该表单中使用 div,则它不起作用(至少它仅适用于第一个 div)。

我想要一个三列布局表单(左、中、右 div 列)。验证仅在左(第一)列中有效。如果我加入所有列,它会再次完美运行。它在第一个关闭 div 后停止工作...我想验证何时单击“提交”按钮。

<script type="text/javascript">
    $(document).ready(function() {
        // Validate signup form on keyup and submit
        var validator = $("#form").validate({
            rules: {
                name: {required: true,},
                age:  {required: true,},
                city: {required: true,}
            },
            messages: {
                name: {required: "required"},
                age:  {required: "required"},
                city: {required: "required"}
            },
        });
    });
</script>

<div class="left">
    <form id="form" class="form" action="REGISTER.PRO" method="post">
        <input id="name" name="name" value="" type="text" class="name"/>
        </div>
        <div class="center">
            <input id="age" name="age" value="" type="text" class="age"/>
        </div>
        <div class="right">
            <input id="city" name="city" value="" type="text" class="city"/>
            <button>Register</button>
    </form>
</div>

我该怎么做?

I am using a fantastic jQuery validation plugin. It is working well with a single form, but if I use divs into this form, it does not work (at least it works only for the first div).

I would like a three-column layout form (left, center & right div columns). Validations works well only in left (first) column. If I join all columns, it works again perfectly. It stops working after the first closing div... I would like to validate when the click submit button is clicked.

<script type="text/javascript">
    $(document).ready(function() {
        // Validate signup form on keyup and submit
        var validator = $("#form").validate({
            rules: {
                name: {required: true,},
                age:  {required: true,},
                city: {required: true,}
            },
            messages: {
                name: {required: "required"},
                age:  {required: "required"},
                city: {required: "required"}
            },
        });
    });
</script>

<div class="left">
    <form id="form" class="form" action="REGISTER.PRO" method="post">
        <input id="name" name="name" value="" type="text" class="name"/>
        </div>
        <div class="center">
            <input id="age" name="age" value="" type="text" class="age"/>
        </div>
        <div class="right">
            <input id="city" name="city" value="" type="text" class="city"/>
            <button>Register</button>
    </form>
</div>

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2024-12-09 03:45:45

您有无效的标记(错误嵌套的 div)和无效的 JavaScript 代码(尾随逗号)。修复方法如下:

<form id="form">
    <div class="left">
        <input id="name" name="name" value="" type="text" class="name"/>
    </div>
    <div class="center">
        <input id="age" name="age" value="" type="text" class="age"/>
    </div>
    <div class="right">
        <input id="city" name="city" value="" type="text" class="city"/>
    </div>
    <button>Register</button>
</form>

JavaScript 代码:

$("#form").validate({
    rules: {
        name: {
            required: true
        },
        age: {
            required: true
        },
        city: {
            required: true
        }
    },
    messages: {
        name: {
            required: "required"
        },
        age: {
            required: "required"
        },
        city: {
            required: "required"
        }
    }
});

以及 现场演示

You have invalid markup (wrongly nested divs) and invalid JavaScript code (trailing commas). Here's how to fix it:

<form id="form">
    <div class="left">
        <input id="name" name="name" value="" type="text" class="name"/>
    </div>
    <div class="center">
        <input id="age" name="age" value="" type="text" class="age"/>
    </div>
    <div class="right">
        <input id="city" name="city" value="" type="text" class="city"/>
    </div>
    <button>Register</button>
</form>

And JavaScript code:

$("#form").validate({
    rules: {
        name: {
            required: true
        },
        age: {
            required: true
        },
        city: {
            required: true
        }
    },
    messages: {
        name: {
            required: "required"
        },
        age: {
            required: "required"
        },
        city: {
            required: "required"
        }
    }
});

And a live demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文