JQuery realizes dynamic operation table

  • 2021-07-10 18:15:09
  • OfStack

The most recent thing to do is to dynamically add and delete rows to a table, and verify the contents of the table without nullness.


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function () {
// Gets the number of rows in the table 
var tabRowLen = $("table tbody tr").length;
// Click add Button, 
$("#add").on("click", function () {
// Gets the number of rows in the table 
tabRowLen = $("table tbody tr").length;
var index = tabRowLen - 1;
// The number of rows in the table is 0 Or when there is no null value in the table 
if (IsNull(index) || tabRowLen == 0) {
// Add 1 Row 
$("table tbody").append("<tr>" +
"<td><input type='text' class='Name'/><div id='dName" + tabRowLen + "'></div></td>" +
"<td><input type='text' class='Age'/><div id='dAge" + tabRowLen + "'></div></td>" +
"<td><input type='button' class='add' value='delete ' /></td></tr>");
// Delete 1 Row 
$(".add").on("click", function () {
$(this).parents("tr").remove();
});
}
//keyup Events 
$("table input").on("keyup", function () {
// Verify that there is a null value 
IsNull(index);
});
});
function IsNull(trIndex) {
var result = true;
debugger;
// Traversing the table input
$("table tbody input").each(function (trIndex) {
// Determine whether there is a null value 
if ($("table tbody input")[trIndex].value == "") {
// Prompt for null value 
result = false;
$(this).next().html("required");
}
// Not empty 
else {
// Empty prompt information 
$(this).next().html("");
}
});
return result;
};
});
</script>
</head>
<body>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th><input type="button" id="add" value="addRow " /></th>
</tr>
</thead>
<tbody></tbody>
</table>
</body>
</html>

Related articles: