JQuery进行添加删除
2017-12-05 16:17
399 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/jquery-1.11.0.min.js"></script>
<script>
$(function() {
// $(".btn_del").on('click', function() {
// alert("123");
// })
$(document).on('click', '.btn_del', function() {
$(this).parent().parent().remove()
var tr = $("tr");
if(tr.length == 1) {
$(".table").hide()
}
})
})
</script>
<style>
.table {
width: 700px;
}
.table tr th {
text-align: center;
color: red;
background-color: yellow;
}
.table td {
border: 1px solid black;
text-align: center;
width: 140px;
}
</style>
</head>
<body>
<hr /> 用户
<input type="text" class="user" id="user" />密码<input type="password" class="pwd" id="pwd" /> 邮箱
<input type="text" class="eml" id="eml" /><input type="button" id="btn" value="添加" />
<input type="button" value="批量删除" id="ip_del" />
<hr />
<table class="table" cellpadding="0px" cellspacing="0px">
<tr id="tr">
<th><input type="checkbox" id="cb_one" class="cb_one" /></th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>操作</th>
</tr>
</table>
<script>
$(".table").hide();
$("#btn").click(function() {
$(".table").show();
var user = $("#user").val();
var pwd = $("#pwd").val();
var eml = $("#eml").val();
var $tr = $("<tr><td><input type='checkbox' class='cb_ins'/></td><td>" + user + "</td><td>" + pwd + "</td><td>" + eml + "</td><td><button class='btn_del'>删除</button></td></tr>")
$(".table").append($tr);
/*$(".btn_del").on('click', function() {
$(this).parent().parent().remove()
})*/
})
var flag = true;
$("#cb_one").click(function() {
var aa = $("input[type=checkbox]")
for(i = 0; i < aa.length; i++) {
aa[i].checked = flag;
}
flag = !flag;
})
$("#ip_del").click(function() {
var $tr = $("input[type=checkbox]:checked");
if($tr.length == 0) {
alert("至少选一行")
}
for(i = 0; i < $tr.length; i++) {
if(i == 0) {
var td = $($tr[i])
var aa = td.parent().parent();
if(aa.attr("id") != "tr") {
alert(aa)
td.parent().parent().remove();
}
} else {
$tr[i].parentNode.parentNode.remove();
}
}
var $tr = $("input[type=checkbox]");
if($tr.length == 1) {
$(".table").hide();
}
})
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/jquery-1.11.0.min.js"></script>
<script>
$(function() {
// $(".btn_del").on('click', function() {
// alert("123");
// })
$(document).on('click', '.btn_del', function() {
$(this).parent().parent().remove()
var tr = $("tr");
if(tr.length == 1) {
$(".table").hide()
}
})
})
</script>
<style>
.table {
width: 700px;
}
.table tr th {
text-align: center;
color: red;
background-color: yellow;
}
.table td {
border: 1px solid black;
text-align: center;
width: 140px;
}
</style>
</head>
<body>
<hr /> 用户
<input type="text" class="user" id="user" />密码<input type="password" class="pwd" id="pwd" /> 邮箱
<input type="text" class="eml" id="eml" /><input type="button" id="btn" value="添加" />
<input type="button" value="批量删除" id="ip_del" />
<hr />
<table class="table" cellpadding="0px" cellspacing="0px">
<tr id="tr">
<th><input type="checkbox" id="cb_one" class="cb_one" /></th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>操作</th>
</tr>
</table>
<script>
$(".table").hide();
$("#btn").click(function() {
$(".table").show();
var user = $("#user").val();
var pwd = $("#pwd").val();
var eml = $("#eml").val();
var $tr = $("<tr><td><input type='checkbox' class='cb_ins'/></td><td>" + user + "</td><td>" + pwd + "</td><td>" + eml + "</td><td><button class='btn_del'>删除</button></td></tr>")
$(".table").append($tr);
/*$(".btn_del").on('click', function() {
$(this).parent().parent().remove()
})*/
})
var flag = true;
$("#cb_one").click(function() {
var aa = $("input[type=checkbox]")
for(i = 0; i < aa.length; i++) {
aa[i].checked = flag;
}
flag = !flag;
})
$("#ip_del").click(function() {
var $tr = $("input[type=checkbox]:checked");
if($tr.length == 0) {
alert("至少选一行")
}
for(i = 0; i < $tr.length; i++) {
if(i == 0) {
var td = $($tr[i])
var aa = td.parent().parent();
if(aa.attr("id") != "tr") {
alert(aa)
td.parent().parent().remove();
}
} else {
$tr[i].parentNode.parentNode.remove();
}
}
var $tr = $("input[type=checkbox]");
if($tr.length == 1) {
$(".table").hide();
}
})
</script>
</body>
</html>
相关文章推荐
- jQuery来了--进行CSS操作的方法(添加/删除类),CSS()方法
- jQuery控制table进行添加和删除
- jQuery选择器进行选择添加和删除样式
- jQuery动态对表格Table进行添加或删除行以及修改列值操作
- web前端,jquery对元素及属性进行获取,设置,添加,删除
- Jquery对表格进行操作——添加行、删除行和绑定行
- jQuery输入框点击添加在下面列表显示可以进行删除
- 基于jquery实现select选择框内容左右移动添加删除代码分享
- Jquery 的 添加 全选 删除 全删
- JQuery动态给table添加、删除行 改进版
- jQuery动态添加删除select项
- 利用jquery给指定的table动态添加一行、删除一行
- JavaScript(jQuery)动态添加/删除html列表
- jQuery操作Select选择的Text和Value(获取/设置/添加/删除)
- 用jquery中动态添加删除class制作导航栏
- jquery表格动态添加删除
- jquery 实现表格内的tr与td,动态添加与删除. 以及序号增加与减少,大多用在Form里
- jquery 添加或删除HTML属性
- 利用jQuery来动态为属性添加或者删除属性