简体   繁体   English

如何在一页上的同一表单的多个副本上使用jQuery验证

[英]How to use jQuery validation on several copies of the same form on one page

The goal of this form is to give a user the ability to respond to questions that people ask. 此表单的目的是使用户能够回答人们提出的问题。 A user will have several questions with response fields underneath. 用户将在下面有几个带有响应字段的问题。 I want to use the jQuery validation plugin to validate that they enter text into the response field and accept the terms. 我想使用jQuery验证插件来验证他们是否在响应字段中输入了文本并接受了条款。

My current solution successfully validates the first form (responding to question #1) but the subsequent questions with response forms don't seem to have the jQuery validations applying to them. 我当前的解决方案成功地验证了第一种形式(对问题#1的回答),但随后的具有响应形式的问题似乎没有适用于它们的jQuery验证。

I have applied a class to the forms and added an index number to the end of each field name to make them unique and I'm still getting the same issue. 我已将一个类应用于表单,并在每个字段名称的末尾添加了一个索引号,以使其具有唯一性,但我仍然遇到同样的问题。

Fiddle 小提琴

HTML HTML

    <div class="form">
    <p>Question #1 text</p>
    <form id="question_response" class="question_response">
        <input type="text" name="response_1"></input>
        <input type="submit">
    </form>
</div>
<div class="form">
    <p>Question #2 text</p>
    <form id="question_response">
        <input type="text" name="response_2"></input>
        <input type="submit">
    </form>
</div>
<div class="form">
    <p>Question #3 text</p>
    <form id="question_response">
        <input type="text" name="response_3"></input>
        <input type="submit">
    </form>
</div>

JS JS

$(function () {
    $(".question_response").validate({
        rules: {
            "response_1": {
                required: true
            },
            "response_2": {
                required: true
            },
            "response_3": {
                required: true
            }
        },
        messages: {
            "response": {
                required: 'This field is required'
            }
        },
        errorPlacement: function (error, element) {
            error.insertAfter(element.parent());
        }
    });
});

Try use class and set unique ID for each <input> element 尝试使用类并为每个<input>元素设置唯一的ID

<form class="question_response">
        <input type="text" name="response"></input>
        <input type="submit">
</form>

 $(".question_response").validate({

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用相同的 function 在一个 web 页面上运行多个视频? - How to use same function to run several videos on one web page? jquery表单验证和php脚本在一个页面中 - jquery form validation and php script in one page 如何在同一页面上使用多个图像滑块? - How to use several image sliders on the same page? 如何使用jQuery从同一页面上的数据库记录填充表单 - How to use jquery to populate a form from database record on the same page 使用jQuery在页面上提交1个Ajax表单,其中包含几种独特的表单 - Use jQuery to submit 1 Ajax form on page with several unique forms 对一个表单输入多次使用一个jQuery函数 - Use one jQuery function multiple times for several form inputs 如何使用带有单选按钮的jquery-plugin“内联表单验证引擎”(需要一个选项)? - How does one use the jquery-plugin “inline form validation engine” with radio buttons (one option required)? 使用$ _POST无法将我的表单(使用jQuery验证)提交到同一页面 - Trouble submitting my form (with jQuery validation) to the same page with $_POST 如何在不更改JS验证格式的情况下验证同一页面中的多个表单 - How to validate more than one form in same page without changing JS validation format jQuery表单:关于提交验证问题+提交后留在同一页面上 - jQuery form: on submit validation issue + stay on the same page after submit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM