如何使用 jquery 验证元素列表
How to validate a list of elements using jquery
这是我的表单,其中包含 childrenDetails 列表。我能够验证除此列表之外的所有其他属性。请帮忙
<form:form commandName="family"
action="${pageContext.request.contextPath}/user/family" method="POST"
id="family-form" novalidate="novalidate">
<table>
<tr>
<c:set var="marraigeStat" value="married" />
<c:if test="${personalDetials.marriageStatus eq marraigeStat}">
<tr>
<td><form:label path="spousesName">Name of Spouse</form:label>
</td>
<td><form:input path="spousesName" id="spousesName" /></td>
</tr>
<tr>
<td><form:label path="spousesAge">Spouses Age</form:label>
<td><form:input path="spousesAge" id="spousesAge" />
</tr>
<tr>
<td><form:label path="weddingDate">Wedding Date</form:label></td>
<td><form:input path="weddingDate" type="date"
name="weddingDate" id="weddingDate" /></td>
<td><form:errors path="weddingDate" /></td>
</tr>
</c:if>
</tr>
<tr>
<td><form:label path="fatherName">Father Name</form:label></td>
<td><form:input path="fatherName" id="fatherName" /></td>
</tr>
<tr>
<td><form:label path="motherName">Mother Name</form:label></td>
<td><form:input path="motherName" id="motherName" /></td>
</tr>
<tr>
<td><form:label path="fatherAge">Father Age</form:label></td>
<td><form:input path="fatherAge" type="date" name="fatherAge"
id="fatherAge" /></td>
</tr>
<tr>
<td><form:label path="motherAge">Mother Age</form:label></td>
<td><form:input path="motherAge" type="date" name="motherAge"
id="motherAge" /></td>
</tr>
<tr>
<c:set var="childerStat" value="true" />
<c:if test="${personalDetials.childerStatus eq childerStat}">
<h5>child should be there</h5>
<c:forEach items="${family.childrenDetails}" varStatus="status">
<tr>
<td><form:label
path="childrenDetails[${status.index}].childName"> Child Name</form:label>
<td><form:input
path="childrenDetails[${status.index}].childName" id="childName" /></td>
<td><form:label
path="childrenDetails[${status.index}].childGender">Child Gender</form:label>
</td>
<td><form:radiobutton
path="childrenDetails[${status.index}].childGender" value="male"
label="male" id="childGender" /></td>
<td><form:radiobutton
path="childrenDetails[${status.index}].childGender"
value="female" label="female" id="childGender" /></td>
<td><form:radiobutton
path="childrenDetails[${status.index}].childGender"
value="others" label="others" id="childGender" /></td>
<td><form:label
path="childrenDetails[${status.index}].childDob">Child Date of Birth</form:label></td>
<td><form:input
path="childrenDetails[${status.index}].childDob" type="date" id="childDob"/></td>
<td><form:label
path="childrenDetails[${status.index}].childCompanyName">Child Company Name</form:label></td>
<td><form:input
path="childrenDetails[${status.index}].childCompanyName" id="childCompanyName" /></td>
<td><form:label
path="childrenDetails[${status.index}].childPosition">Child Position</form:label></td>
<td><form:input
path="childrenDetails[${status.index}].childPosition" id="childPosition" /></td>
</tr>
</c:forEach>
<tr>
<c:if test="${familyDetails.getChildrenDetails().size()!=0 }">
<td><form:button name="submit" value="ADD">ADD</form:button></td>
<td><form:button name="submit" value="REMOVE">REMOVE</form:button></td>
</c:if>
</tr>
</c:if>
<c:if test="${family.childrenDetails != null }">
<tr>
<td><h2>CHILD DISPLAY</h2></td>
<tr>
<th>Child Name</th>
<th>childGender</th>
<th>childDob</th>
<th>childCompanyName</th>
<th>childPosition</th>
</tr>
<tr>
<c:forEach items="${family.childrenDetails}" var="child"
varStatus="status">
<tr>
<td>${child.childName}</td>
<td>${child.childGender}</td>
<td>${child.childDob}</td>
<td>${child.childCompanyName}</td>
<td>${child.childPosition}</td>
</tr>
</c:forEach>
</tr>
</c:if>
</tr>
在可用的代码中,我必须了解我已为列表的每个属性分配 ID 的子项详细信息。
现在,在这个 ID 的帮助下,我正在尝试验证这些字段。
我的验证不仅仅适用于家庭内部的 childrenDetails 列表。
这是我的 javascript 验证
$(function() {
$("#family-form").validate({
rules : {
spousesName : "required",
spousesAge : "required",
weddingDate : "required",
fatherName : "required",
motherName : "required",
fatherAge : "required",
motherAge : "required",
childName : "required",
childGender : "required",
childDob : "required",
childCompanyName : "required",
childPosition : "required"
},
messages : {
spousesName : "please enter your spouses name",
spousesAge : "Please enter the age of your spouses",
weddingDate : "what is your wedding date",
fatherName : "please enter your father name",
motherName : "please enter your mother name",
fatherAge : "please enter your father age",
motherAge : "please enter your mothers age",
childName : "required",
childGender : "required",
childDob : "required",
childCompanyName : "required",
childPosition : "required"
},
submitHandler : function(form) {
form.submit();
}
});
});
function goBack(){
window.history.back();
}
请帮忙。提前致谢
验证不工作可能有两个原因。
id
的独特性:主要问题
- 动态元素:JavaScript 正在创建新的子详细信息。即在添加按钮后生成新的表单块:可能是问题
案例联合国唯一性
好的,问题出在DOM中id
的联合国唯一性。因此,基本原则是 id
属性在整个渲染文档中应该是唯一的。
<c:forEach items="${family.childrenDetails}" varStatus="status">
<tr>
<td>
<form:label path="childrenDetails[${status.index}].childName">Child Name</form:label>
<td>
<form:input path="childrenDetails[${status.index}].childName" id="childName" />
</td>
<td>
<form:label path="childrenDetails[${status.index}].childGender">Child Gender</form:label>
//So In this block childName id will be duplicate if more than one children exists
因此,以下是您在 id
重复时面临的类似问题的示例:
JSFIDDLE DEMO 1
因此,解决方案是使用 class
选择器而不是 id
进行验证。您可以将其用作:
$('#family-form').validate({
rules : {
spousesName : "required",
spousesAge : "required",
weddingDate : "required",
fatherName : "required",
motherName : "required",
fatherAge : "required",
motherAge : "required",
},
messages : {
spousesName : "please enter your spouses name",
spousesAge : "Please enter the age of your spouses",
weddingDate : "what is your wedding date",
fatherName : "please enter your father name",
motherName : "please enter your mother name",
fatherAge : "please enter your father age",
motherAge : "please enter your mothers age",
},
submitHandler : function(form) {
form.submit();
}
});
$('.childNameValidation').each(function() {
$(this).rules('add', {
required: true,
messages: {
required: "Child Name is invalid",
}
});
});
虽然为每个其他元素定义这样的元素可能会很忙。但是,它应该可以完成工作。
需要包括:http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/additional-methods.js
JSFIDDLE DEMO 2
CASE 动态元素
请按此操作:Adding jQuery validator rules to dynamically created elements in ASP
针对该问题的不同方法
解决此问题的最简单方法可能是在表单元素本身上使用 data-attribute
:
参见:Declaring jQuery Validate plugin rules -- attribute vs. class vs. code
上面提到的解决方案只是我的直觉猜测。因此,它可能会或可能不会解决您面临的问题。
这是我的表单,其中包含 childrenDetails 列表。我能够验证除此列表之外的所有其他属性。请帮忙
<form:form commandName="family"
action="${pageContext.request.contextPath}/user/family" method="POST"
id="family-form" novalidate="novalidate">
<table>
<tr>
<c:set var="marraigeStat" value="married" />
<c:if test="${personalDetials.marriageStatus eq marraigeStat}">
<tr>
<td><form:label path="spousesName">Name of Spouse</form:label>
</td>
<td><form:input path="spousesName" id="spousesName" /></td>
</tr>
<tr>
<td><form:label path="spousesAge">Spouses Age</form:label>
<td><form:input path="spousesAge" id="spousesAge" />
</tr>
<tr>
<td><form:label path="weddingDate">Wedding Date</form:label></td>
<td><form:input path="weddingDate" type="date"
name="weddingDate" id="weddingDate" /></td>
<td><form:errors path="weddingDate" /></td>
</tr>
</c:if>
</tr>
<tr>
<td><form:label path="fatherName">Father Name</form:label></td>
<td><form:input path="fatherName" id="fatherName" /></td>
</tr>
<tr>
<td><form:label path="motherName">Mother Name</form:label></td>
<td><form:input path="motherName" id="motherName" /></td>
</tr>
<tr>
<td><form:label path="fatherAge">Father Age</form:label></td>
<td><form:input path="fatherAge" type="date" name="fatherAge"
id="fatherAge" /></td>
</tr>
<tr>
<td><form:label path="motherAge">Mother Age</form:label></td>
<td><form:input path="motherAge" type="date" name="motherAge"
id="motherAge" /></td>
</tr>
<tr>
<c:set var="childerStat" value="true" />
<c:if test="${personalDetials.childerStatus eq childerStat}">
<h5>child should be there</h5>
<c:forEach items="${family.childrenDetails}" varStatus="status">
<tr>
<td><form:label
path="childrenDetails[${status.index}].childName"> Child Name</form:label>
<td><form:input
path="childrenDetails[${status.index}].childName" id="childName" /></td>
<td><form:label
path="childrenDetails[${status.index}].childGender">Child Gender</form:label>
</td>
<td><form:radiobutton
path="childrenDetails[${status.index}].childGender" value="male"
label="male" id="childGender" /></td>
<td><form:radiobutton
path="childrenDetails[${status.index}].childGender"
value="female" label="female" id="childGender" /></td>
<td><form:radiobutton
path="childrenDetails[${status.index}].childGender"
value="others" label="others" id="childGender" /></td>
<td><form:label
path="childrenDetails[${status.index}].childDob">Child Date of Birth</form:label></td>
<td><form:input
path="childrenDetails[${status.index}].childDob" type="date" id="childDob"/></td>
<td><form:label
path="childrenDetails[${status.index}].childCompanyName">Child Company Name</form:label></td>
<td><form:input
path="childrenDetails[${status.index}].childCompanyName" id="childCompanyName" /></td>
<td><form:label
path="childrenDetails[${status.index}].childPosition">Child Position</form:label></td>
<td><form:input
path="childrenDetails[${status.index}].childPosition" id="childPosition" /></td>
</tr>
</c:forEach>
<tr>
<c:if test="${familyDetails.getChildrenDetails().size()!=0 }">
<td><form:button name="submit" value="ADD">ADD</form:button></td>
<td><form:button name="submit" value="REMOVE">REMOVE</form:button></td>
</c:if>
</tr>
</c:if>
<c:if test="${family.childrenDetails != null }">
<tr>
<td><h2>CHILD DISPLAY</h2></td>
<tr>
<th>Child Name</th>
<th>childGender</th>
<th>childDob</th>
<th>childCompanyName</th>
<th>childPosition</th>
</tr>
<tr>
<c:forEach items="${family.childrenDetails}" var="child"
varStatus="status">
<tr>
<td>${child.childName}</td>
<td>${child.childGender}</td>
<td>${child.childDob}</td>
<td>${child.childCompanyName}</td>
<td>${child.childPosition}</td>
</tr>
</c:forEach>
</tr>
</c:if>
</tr>
在可用的代码中,我必须了解我已为列表的每个属性分配 ID 的子项详细信息。 现在,在这个 ID 的帮助下,我正在尝试验证这些字段。 我的验证不仅仅适用于家庭内部的 childrenDetails 列表。 这是我的 javascript 验证
$(function() {
$("#family-form").validate({
rules : {
spousesName : "required",
spousesAge : "required",
weddingDate : "required",
fatherName : "required",
motherName : "required",
fatherAge : "required",
motherAge : "required",
childName : "required",
childGender : "required",
childDob : "required",
childCompanyName : "required",
childPosition : "required"
},
messages : {
spousesName : "please enter your spouses name",
spousesAge : "Please enter the age of your spouses",
weddingDate : "what is your wedding date",
fatherName : "please enter your father name",
motherName : "please enter your mother name",
fatherAge : "please enter your father age",
motherAge : "please enter your mothers age",
childName : "required",
childGender : "required",
childDob : "required",
childCompanyName : "required",
childPosition : "required"
},
submitHandler : function(form) {
form.submit();
}
});
});
function goBack(){
window.history.back();
}
请帮忙。提前致谢
验证不工作可能有两个原因。
id
的独特性:主要问题- 动态元素:JavaScript 正在创建新的子详细信息。即在添加按钮后生成新的表单块:可能是问题
案例联合国唯一性
好的,问题出在DOM中id
的联合国唯一性。因此,基本原则是 id
属性在整个渲染文档中应该是唯一的。
<c:forEach items="${family.childrenDetails}" varStatus="status">
<tr>
<td>
<form:label path="childrenDetails[${status.index}].childName">Child Name</form:label>
<td>
<form:input path="childrenDetails[${status.index}].childName" id="childName" />
</td>
<td>
<form:label path="childrenDetails[${status.index}].childGender">Child Gender</form:label>
//So In this block childName id will be duplicate if more than one children exists
因此,以下是您在 id
重复时面临的类似问题的示例:
JSFIDDLE DEMO 1
因此,解决方案是使用 class
选择器而不是 id
进行验证。您可以将其用作:
$('#family-form').validate({
rules : {
spousesName : "required",
spousesAge : "required",
weddingDate : "required",
fatherName : "required",
motherName : "required",
fatherAge : "required",
motherAge : "required",
},
messages : {
spousesName : "please enter your spouses name",
spousesAge : "Please enter the age of your spouses",
weddingDate : "what is your wedding date",
fatherName : "please enter your father name",
motherName : "please enter your mother name",
fatherAge : "please enter your father age",
motherAge : "please enter your mothers age",
},
submitHandler : function(form) {
form.submit();
}
});
$('.childNameValidation').each(function() {
$(this).rules('add', {
required: true,
messages: {
required: "Child Name is invalid",
}
});
});
虽然为每个其他元素定义这样的元素可能会很忙。但是,它应该可以完成工作。
需要包括:http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.0/additional-methods.js
JSFIDDLE DEMO 2
CASE 动态元素
请按此操作:Adding jQuery validator rules to dynamically created elements in ASP
针对该问题的不同方法
解决此问题的最简单方法可能是在表单元素本身上使用 data-attribute
:
参见:Declaring jQuery Validate plugin rules -- attribute vs. class vs. code
上面提到的解决方案只是我的直觉猜测。因此,它可能会或可能不会解决您面临的问题。