<html>
<head>
<title>添加、删除一行</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(function() {
$("#addOneRow").click(function() {
var tempTr = $("tr:first").clone(true);
$("tr:last").after(tempTr);
$("tr:last > td > #name").val("");//新添加行name为空
$("tr:last > td > #address").val("");//新添加行address为空
});
$(".delOneRow").click(function() {
if ($("tr").length < 2) {
alert("至少保留一行!");
} else {
if (confirm("确认删除?")) {
// $(this).parent().parent().remove();
$(this).parents("tr").remove();
}
}
});
});
</script>
</head>
<body>
<table border="1">
<tr>
<td>姓名:</td>
<td><input type="text" id="name" name="name" />
</td>
<td>地址:</td>
<td><input type="text" id="address" name="address" /></td>
<td><input type="button" class="delOneRow" value="删除" /></td>
</tr>
</table>
<input type="button" id="addOneRow" value="添加一行" />
</body>
</html>jquery动态添加删除一行数据
最新推荐文章于 2021-06-24 05:40:58 发布
本文介绍了一种使用jQuery简化操作的方法,在网页中实现表格行的动态添加与删除功能。通过点击按钮即可轻松完成行的增加或删除,并确保至少保留一行以避免误操作。
198

被折叠的 条评论
为什么被折叠?



