亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JQuery實現(xiàn)動態(tài)操作表格

asal 2017-01-13 13:12:41 274
abstrak:最近要做的東西,是對一個表格動態(tài)的添加行,刪除行,并且對表格中內(nèi)容進行非空驗證。<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" 

最近要做的東西,是對一個表格動態(tài)的添加行,刪除行,并且對表格中內(nèi)容進行非空驗證。

<!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 () {
//獲取表格的行數(shù)
var tabRowLen = $("table tbody tr").length;
//點擊add按鈕時,
$("#add").on("click", function () {
//獲取表格的行數(shù)
tabRowLen = $("table tbody tr").length;
var index = tabRowLen - 1;
//表格行數(shù)為0時,或者表格不存在空值時
if (IsNull(index) || tabRowLen == 0) {
//添加一行
$("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>");
//刪除一行
$(".add").on("click", function () {
$(this).parents("tr").remove();
});
}
//keyup事件
$("table input").on("keyup", function () {
//驗證是否有空值
IsNull(index);
});
});
function IsNull(trIndex) {
var result = true;
debugger;
//遍歷表格的input
$("table tbody input").each(function (trIndex) {
//判斷是否存在空值
if ($("table tbody input")[trIndex].value == "") {
//提示空值
result = false;
$(this).next().html("required");
}
//不為空
else {
//清空提示信息
$(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>

更多關(guān)于JQuery實現(xiàn)動態(tài)操作表格請關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!

Nota Keluaran

Penyertaan Popular