jquery 模态对话框传值,删除,新增表格行
个人的练习代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<style>
body{
margin: 0;
padding: 0;
} .cover{
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #b0b0b0;
opacity: 0.4;
z-index: 2;
} .modal{
width: 500px;
height: 400px;
position: fixed;
z-index: 3;
background-color: white;
left: 50%;
top: 40%;
margin-left: -250px;
margin-top: -200px; } .hide{
display: none;
}
</style>
</head>
<body>
<div class="cover hide"></div> <div class="modal hide">
<div>
<label for="name">姓名</label><input type="text" id="name">
</div>
<div>
<label for="hobby">爱好</label><input type="text" id="hobby">
</div>
<button id="cancel">取消</button>
<button id="submit">提交</button>
</div> <button id="add">新增</button> <table border="1">
<thead>
<tr>
<td>#</td>
<td>姓名</td>
<td>爱好</td>
<td colspan="2" style="text-align: center">操作</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td>小明</td>
<td id="t1">抽烟,喝酒,烫头</td>
<td><button class="move">删除</button></td>
<td><button class="edit">编辑</button></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>贝吉塔</td>
<td>修行,找布尔玛吃东西</td>
<td><button class="move">删除</button></td>
<td><button class="edit">编辑</button></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>孙悟空</td>
<td>吃饭去界王星修炼</td>
<td><button class="move">删除</button></td>
<td><button class="edit">编辑</button></td>
</tr>
</tbody>
</table> <script> // 新增按钮
$("#add").click(function () { $("#name,#hobby").prop("value","");
$(".cover,.modal").removeClass("hide");
}); // 取消按钮
$("#cancel").click(function () {
$(".cover,.modal").addClass("hide");
}); //删除行,用到事件委托,主要是因为新增的行不会自动添加按钮事件
$("tbody").on("click",".move",function () {
$(this).parent().parent().remove();
}); //提交
$("#submit").click(function () { var name = $("#name").val();
var hobby = $("#hobby").val(); if($("#submit").data("k")){
bt_edit = $("#submit").data("k");
bt_edit.parent().prev().prev().prev().text(name);
bt_edit.parent().prev().prev().text(hobby);
}else{
var s = "<tr>" +
" <td><input type=\"checkbox\"></td>" +
" <td>"+name+"</td>" +
" <td>"+hobby+"</td>" +
" <td><button class=\"move\">删除</button></td>" +
"<td><button>编辑</button></td></tr>";
$("tbody").append(s); }
$(".cover,.modal").addClass("hide");
$("#submit").data("k",""); }); //编辑
$("tbody").on("click",".edit",function () {
//设定一个标志位
$("#submit").data("k",$(this));
var name = $(this).parent().prev().prev().prev().text();
var hobby = $(this).parent().prev().prev().text();
console.log(name);
$("#name").val(name);
$("#hobby").val(hobby);
$(".cover,.modal").removeClass("hide"); })
</script>
</body>
</html>
完善版
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.shadow{
position: fixed;
background-color: rgba(0,0,0,0.3);
top:0;
left:0;
bottom: 0;
right:0;
z-index: 999;
} .modal{
position: fixed;
width: 400px;
height: 200px;
background-color: #ffffff;
top:50%;
left: 50%;
z-index: 1000;
margin-top: -100px;
margin-left: -200px;
} .hide{
display: none;
} </style>
<script>
// window.onload = function () {
//
// alert('xx')
// } </script>
</head>
<body> <button id="all">全选</button>
<button id="reverse">反选</button>
<button id="cancel">取消</button>
<button id="add">新增</button>
<table border="1">
<thead>
<tr>
<th>#</th>
<th>姓名</th>
<th>爱好</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox"></td>
<td>1</td>
<td>1</td>
<td><button class="b1">删除</button><button class="edit">编辑</button></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>2</td>
<td>2</td>
<td><button class="b1">删除</button><button class="edit">编辑</button></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td>3</td>
<td>3</td>
<td><button class="b1">删除</button><button class="edit">编辑</button></td>
</tr> </tbody>
</table> <div class="modal hide">
<div>
<label for="name">姓名</label>
<input id="name" type="text">
</div> <div>
<label for="hobby">爱好</label>
<input id="hobby" type="text">
</div>
<div>
<button class="btn2">保存</button>
</div> </div> <div class="shadow hide"></div> <script src="jquery.js"></script>
<script>
// 全选
$("#all").click(function () {
$(":checkbox").prop('checked', true);
});
// 取消
$("#cancel").on("click", function () {
$(":checkbox").prop('checked', false);
});
// 反选
$("#reverse").click(function () { var $checkbox = $(":checkbox");
for (var i=0;i<$checkbox.length;i++){
// 获取原来的选中与否的状态
var status = $($checkbox[i]).prop('checked');
$($checkbox[i]).prop('checked', !status);
} }); //点击新增,弹出模态框
$('#add').click(function () {
// $('.modal').removeClass('hide');
// $('.shadow').removeClass('hide'); $('.modal,.shadow').removeClass('hide'); }); var editBtn ; //保存点击的编辑按钮对象
//因为是新添加的编辑按钮,所以需要事件委托来绑定click事件
$('tbody').on('click','.edit',function () {
//1 弹出模态框
$('.modal,.shadow').removeClass('hide');
//2 获取和编辑按钮同一行的姓名和爱好对应的单元格的内容
var nameEle = $(this).parent().siblings().eq(1);
var hobbyEle = $(this).parent().siblings().eq(2);
var name = nameEle.text();
var hobby = hobbyEle.text();
console.log(name,hobby);
// 3 将获取的信息,添加到对应的input框里面
$('#name').val(name);
$('#hobby').val(hobby);
// 4 保存修改的信息到原来的位置
// $('tbody').data('k1',$(this))
editBtn = $(this); }); //点击保存
$('.btn2').click(function () {
//1 获取用户输入的信息
var name = $('#name').val();
var hobby = $('#hobby').val();
//如果editBtn = undefined 说明是新增
// if ($('tbody').data('k1') === undefined){
if (editBtn === undefined){
//2 创建标签,将数据添加到标签里面,拼接字符串添加标签
var s = "<tr>" +
"<td><input type='checkbox'></td>" +
"<td>" + name + "</td>" +
"<td>" + hobby + "</td>" +
"<td><button class='b1'>删除</button><button class='edit'>编辑</button></td>" +
"</tr>";
// var s = "<tr class='cc'></tr>"
//3 将创建好的标签,添加到表格里面去 $('tbody').append(s);
//4 隐藏对话框 }
else {
//通过全局的那个editBtn(表示的是编辑按钮对象本身),来找到对应这个编辑按钮的那一行的两个td标签的对象
var nameEle =editBtn.parent().siblings().eq(1);
var hobbyEle = editBtn.parent().siblings().eq(2);
//x修改原来内容
nameEle.text(name);
hobbyEle.text(hobby);
//将全局变量还原为undefined
editBtn = undefined;
} $('.modal,.shadow').addClass('hide');
//5 清空用户输入的内容
$('#name').val('');
$('#hobby').val(''); }); $('tbody').on('click','.b1',function () {
$(this).parent().parent().remove();
}); window.onload = function () { } </script>
</body>
</html>
<script>
//全选
$('#all').click(function () {
//找到所有的checkbox标签,并添加checked属性
$(':checkbox').prop('checked',true); });
//取消
$('#cancel').click(function () {
//找到所有的checkbox标签,并删除checked属性
$(':checkbox').prop('checked',false);
});
//反选
$('#reverse').click(function () {
// $('input:checkbox:not(:checked)').prop('checked',true);
// $(':checked').prop('checked',false);
$(':checkbox').each(function () {
//找到标签的checked属性的状态,如果是true,改成false
var status = $(this).prop('checked');
$(this).prop('checked',!status);
// if (status){
// $(this).prop('checked',false);
// }
// else {
// $(this).prop('checked',true);
// } }) }) </script>
jquery 模态对话框传值,删除,新增表格行的更多相关文章
- jquery综合练习--模态对话框传值,删除,新增表格行
效果示例: 个人的练习代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- 15款基于 jQuery模态对话框
在数字世界的竞争已大大增加.这就是为什么要确保网络设计的各个方面都是一流的,这是很重要的.从布局到一些非常小的东西,比如对话框,每一件都需要设计得很好.对话框通常被忽视,但它们可能对访问者有很大的影响 ...
- JQuery EasyUI DataGrid根据条件设置表格行样式(背景色)
1.javascript定义函数返回样式 <script type="text/javascript"> //根据条件设置表格行背景颜色 function setRow ...
- 使用jQuery通过点击它删除HTML表格行-超简单
jQuery的已成为所有时刻的最常用和最喜爱的JavaScript框架之一.它不仅不会减少在JavaScript编码简单的技术开销,而且也使您的代码的跨浏览器兼容.我已经写了许多关于jQuery教程, ...
- 动态新增删除tbody表格行与ajax请求完成后刷新父窗口问题
获取tbody内的一行数据,包括hidden类型的数据$("#tbody_id").find("tr").each(function(){ var tdArr ...
- jQuery 模态对话框示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 删除table表格行
function getRowObj(obj) { while(obj.tagName.toLowerCase()!="tr") // toLowerCase转化小写 { ...
- jQuery实现表格行的动态增加与删除(改进版)
之前写过一个简单的利用jQuery实现表格行的动态增加与删除的例子,有些人评论说"如果表格中是input元素,那么删除后的东西都将自动替换,这样应该是有问题的,建议楼主改进!",故 ...
- jQuery练习 | 模态对话框(添加删除)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- 最新版本的MySQL的下载和安装(Release: 8.0.12)
1.打开百度搜索[Myql],或直达官网https://dev.mysql.com/ 2.点选[Download按钮],跳转到下载页面,拉到底部再点选[Community Download]社区版[免 ...
- qbzt day3 下午(好难)
内容提要 有关数据结构的例题 求逆序对数 统计每个数前面有多少比他大的数 开数组表示这个数之前0~9这些数出现了几次 动态将某个点加一,动态求前缀和 用树状数组 如果数太大了怎么办? 离散化 步骤:先 ...
- principal components analysis 主成份分析
w http://deeplearning.stanford.edu/wiki/index.php/主成份分析 主成分分析(PCA)及其在R里的实现 - jicf的日志 - 网易博客 http:// ...
- Flask中的路由配置
在Flask中也同样有django中的路由配置只不过没有djngo那么严格主要的参数有一下六个记住常用的就可以了 1.endpoint 反向生成url地址标志,默认视图函数名 2.methods ...
- How to derive mean and variance of a Gaussian?
PRML exercise 1.8: To derive mean: change of variable z = x - u, use symmetry To derive variance: di ...
- java 操作hdfs(连接HDFS)
FileSystem fs = null; Configuration conf = null; @Before public void init() throws Exception{ conf = ...
- iOS 命令行打包--xcworkspace
参考: 打包的具体操作步骤: https://www.jianshu.com/p/6a0aa8cd2e97 打包时使用到的参数详解,参考这篇: https://debugtalk.com/post/i ...
- golang md5 结果类型
golang md5 结果类型 package main import ( "crypto/md5" "encoding/hex" "fmt&quo ...
- TensorFlow学习笔记13-循环、递归神经网络
循环神经网络(RNN) 卷积网络专门处理网格化的数据,而循环网络专门处理序列化的数据. 一般的神经网络结构为: 一般的神经网络结构的前提假设是:元素之间是相互独立的,输入.输出都是独立的. 现实世界中 ...
- react 之 flux
[WangQi]---flux---[react] 一.什么是Flux Flux 是一种架构思想,专门解决软件的结构问题.它跟MVC 架构是同一类东西,但是更加简单和清晰. 二.flux的基本概念 ...