[英]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. 我已将一个类应用于表单,并在每个字段名称的末尾添加了一个索引号,以使其具有唯一性,但我仍然遇到同样的问题。
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.