×

JQuery动态给table添加、删除行 改进版

作者:Terry2017.02.10来源:Web前端之家浏览:8591评论:0
关键词:JQueryJS
复制代码 代码如下:

<html>
<head>
<title>
</title>
<script src="js/jquery-1.4.2_min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
var row_count = 0;
function addNew()
{
var table1 = $('#table1');
var firstTr = table1.find('tbody>tr:first');
var row = $("<tr></tr>");
var td = $("<td></td>");
td.append($("<input type='checkbox' name='count' value='New'><b>CheckBox"+row_count+"</b>")
);
row.append(td);
table1.append(row);
row_count++;
}
function del()
{
var checked = $("input[type='checkbox'][name='count']");
$(checked).each(function(){
if($(this).attr("checked")==true) //注意:此处判断不能用$(this).attr("checked")==‘true'来判断。
{
$(this).parent().parent().remove();
}
});
}
</script>
</head>
<body>
<input type="button" value="Add" onclick="addNew();">
<input type="button" value="Delete" onclick="del();">
<div id="rightcontent">
<table id="table1" cellspacing="3" cellpadding="3" border="1">
<tbody>
<tr>
<th>
Add new row,then test the delete function.
</th>
</tr>
</tbody>
</table>
</div>
</body>
</html>

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/svg1486656000581.html

网友评论文明上网理性发言已有0人参与

发表评论: