【发布时间】:2020-11-09 07:29:37
【问题描述】:
我想使用 jQuery 对表中的 <tr> 进行动态验证。必须对每个字段进行空验证。不应使用验证插件。
表格的HTML代码:
<table id="stutdent" class="studentForm" border="0">
<tbody>
<div class="inputField">
<tr class="dynamicRow">
<td>
<b><bean:message key="label.student.code" />:</b>
</td>
<td class="studentCreateSelect">
<html:select property="studentCreate" styleId="studentCreateSelect" >
<html:option value="">
<bean:message key="label.student.code.select" />
</html:option>
<html:option value="A">A</html:option>
<html:option value="B">B</html:option>
<html:optionsCollection name="studentForm" property="studentList" label="label" value="value" />
</html:select>
</td>
</tr>
</div>
<div class="inputField">
<td>
<b><bean:message key="label.student.name" />:</b>
</td>
<td class="sNameList">
<html:text property="sNameList" name="studentForm" styleId="sNameList" size="10" maxlength="6"></html:text>
</td>
</div>
点击Add 时,会动态添加行。我可以对第一行进行验证,但不能动态验证。请帮忙。
我的添加代码:
function addRow() {
var $rowObj = $("#stutdent tr:first");
$rowObj.clone().insertAfter($rowObj);
$('.add_btn', $rowObj).replaceWith('<button id="remove_row" onclick="delRow(this)">Del</button>'); // Remove the button from the previous tr, otherwise each row will have it.
}
Able to validate messages dynamically
My new issue: Messages are not shown under the field but getting displayed next to it
It has to be shown below each field
Kindly help
My validation code :
function validateNewDockGate(fromScreen){
$("tr.dynamicRow").each(function() {
var d = $(this).find(".studentCreateSelect").val();
if(d === "")
valid = false;
$(this).find(".studentCreateSelect").after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please select Student id</div>");
var e = $(this).find(".sNameList").val();
if(e === "")
valid = false;
$(this).find(".sNameList").after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please select Student id</div>");
});
}
【问题讨论】:
-
使用深度克隆,它也会复制偶数处理程序。喜欢
$rowObj.clone(true).insertAfter($rowObj); -
好的..如何为下拉/文本字段添加验证?
-
我的验证代码部分: $("tr.dynamicRow").each(function() { var d = $("#studentCreateSelect").val(); if($("#studentCreateSelect ").val() === "") valid = false; $("#studentCreateSelect").after("请选择学生编号"); });
-
您的第一个问题解决了吗?
-
我的问题是动态添加字段验证。正在添加行,但验证无法动态进行。只需进行表单验证,主要检查任何字段中的值是否不为空。请建议。
标签: jquery validation