转载收藏于:https://www.cnblogs.com/zhangqs008/archive/2013/05/09/3618459.html
<head>
<title>jquery表格操作</title>
<script language="javascript" src="jquery.table.tr.js"></script>
<style type="text/css">
table
{
border: black solid 1px;
border-collapse: collapse;
}
td
{
border: black solid 1px;
padding: 3px;
}
.td_Num
{
width: 60px;
text-align: center;
}
.td_Item
{
width: 160px;
text-align: center;
}
.td_Oper
{
width: 120px;
text-align: center;
}
.td_Oper span
{
cursor: pointer;
}
</style>
</head>
<body>
<table>
<tr>
<td class='td_Num'>
序号
</td>
<td class='td_Item'>
步骤名称
</td>
<td class='td_Item'>
步骤描述
</td>
<td class='td_Oper'>
相关操作 <a href="#" onclick="add_line();">添加</a>
</td>
</tr>
</table>
<table id="content">
</table>
<input type="button" value="提交数据" id="btnSubmit" onclick="SaveData()" />
</body>
</html>
<script type="text/javascript">
var currentStep = 0;
var max_line_num = 0;
//添加新记录
function add_line() {
max_line_num = $("#content tr:last-child").children("td").html();
if (max_line_num == null) {
max_line_num = 1;
}
else {
max_line_num = parseInt(max_line_num);
max_line_num += 1;
}
$('#content').append(
"<tr id='line" + max_line_num + "'>" +
"<td class='td_Num'>" + max_line_num + "</td>" +
"<td class='td_Item'><input type='text' class='stepName' value='步骤名称" + max_line_num + "'></input></td>" +
"<td class='td_Item'><input type='text' class='stepDescription' value='步骤描述" + max_line_num + "'></td>" +
"<td class='td_Oper'>" +
"<span onclick='up_exchange_line(this);'>上移</span> " +
"<span onclick='down_exchange_line(this);'>下移</span> " +
"<span onclick='remove_line(this);'>删除</span> " +
"</td>" +
"</tr>");
}
//删除选择记录
function remove_line(index) {
if (index != null) {
currentStep = $(index).parent().parent().find("td:first-child").html();
}
if (currentStep == 0) {
alert('请选择一项!');
return false;
}
if (confirm("确定要删除改记录吗?")) {
$("#content tr").each(function () {
var seq = parseInt($(this).children("td").html());
if (seq == currentStep) { $(this).remove(); }
if (seq > currentStep) { $(this).children("td").each(function (i) { if (i == 0) $(this).html(seq - 1); }); }
});
}
}
//上移
function up_exchange_line(index) {
if (index != null) {
currentStep = $(index).parent().parent().find("td:first-child").html();
}
if (currentStep == 0) {
alert('请选择一项!');
return false;
}
if (currentStep <= 1) {
alert('已经是最顶项了!');
return false;
}
var upStep = currentStep - 1;
//修改序号
$('#line' + upStep + " td:first-child").html(currentStep);
$('#line' + currentStep + " td:first-child").html(upStep);
//取得两行的内容
var upContent = $('#line' + upStep).html();
var currentContent = $('#line' + currentStep).html();
$('#line' + upStep).html(currentContent);
//交换当前行与上一行内容
$('#line' + currentStep).html(upContent);
$('#content tr').each(function () { $(this).css("background-color", "#ffffff"); });
$('#line' + upStep).css("background-color", "yellow");
event.stopPropagation(); //阻止事件冒泡
}
//下移
function down_exchange_line(index) {
if (index != null) {
currentStep = $(index).parent().parent().find("td:first-child").html();
}
if (currentStep == 0) {
alert('请选择一项!');
return false;
}
if (currentStep >= max_line_num) {
alert('已经是最后一项了!');
return false;
}
var nextStep = parseInt(currentStep) + 1;
//修改序号
$('#line' + nextStep + " td:first-child").html(currentStep);
$('#line' + currentStep + " td:first-child").html(nextStep);
//取得两行的内容
var nextContent = $('#line' + nextStep).html();
var currentContent = $('#line' + currentStep).html();
//交换当前行与上一行内容
$('#line' + nextStep).html(currentContent);
$('#line' + currentStep).html(nextContent);
$('#content tr').each(function () { $(this).css("background-color", "#ffffff"); });
$('#line' + nextStep).css("background-color", "yellow");
event.stopPropagation(); //阻止事件冒泡
}
//保存数据
function SaveData() {
var data = "<root>";
$('#content tr').each(function () {
data += "<item>";
var stepName = $(this).find("td:eq(1)").find("input").val();
var stepDescription = $(this).find("td:eq(2)").find("input").val();
data += " <stepName>" + stepName + "</stepName>";
data += " <stepDescription>" + stepDescription + "</stepDescription>";
data += "<item>";
});
data += "</root>";
alert(data);
}
</script>
- Word:表格前添加新行 + 删除表格后的空行
本文适用于Word 2007 + Windows 7,造冰箱的大熊猫@cnblogs 2018/8/3 近日新学(百度到)两条新Word操作,记录下来以备查询 1.在表格前添加新行 场景:有没有遇到过 ...
- ajax异步获取数据后动态向表格中添加数据(行)
因为某些原因,项目中突然需要做自己做个ajax异步获取数据后动态向表格中添加数据的页面,网上找了半天都没有 看到现成的,决定自己写个例子 1.HTML页面 <!doctype html> ...
- 一个word文档中,多个表格的批量调整(根据窗口调整表格和添加表格水平线)
Sub 自动调整所有表格() ' ' 自动调整所有表格 宏 ' 'Application.Browser.Target = wdBrowseTable For i = 1 To ActiveDocum ...
- jquery 动态添加表格行
jquery 动态添加表格行 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <h ...
- JQuery实现表格动态增加行并对新行添加事件
实现功能: 通常在编辑表格时表格的行数是不确定的,如果一次增加太多行可能导致页面内容太多,反应变慢:通过此程序实现表格动态增加行,一直保持最下面有多个空白行. 效果: 一:原始页面 二:表1增加新行并 ...
- JQuery动态添加表格,然后动态删除不成功问题
背景: 自己做了一个测试网页,想动态添加表格,然后删除,按照网上的教程写完,发现点击"删除参数"按钮没用 源码: function addtr() { var trinfo = & ...
- jQuery动态添加表格1
用jquery的append方法在指定行的后面新增一行tr,会把新增的行的html追加到指定行的html里面 content +="<tr><td>123</t ...
- 基于jquery的表格动态创建,自动绑定,自动获取值
最近刚加入GUT项目,学习了很多其他同事写的代码,感觉受益匪浅. 在GUT项目中,经常会碰到这样一个问题:动态生成表格,包括从数据库中读取数据,并绑定在表格中,以及从在页面上通过jQuery新增删除表 ...
- jQuery动态添加删除与添加表行代码
具体实现代码如下: table的HTML如下: 代码如下 复制代码 <input type="button" value="添加一行" />< ...
- js如何实现动态的在表格中添加和删除行?(两种方法)
js如何实现动态的在表格中添加和删除行?(两种方法) 一.总结 1.table元素有属性和一些方法(js使用) 方法一:添加可通过在table的innerHTML属性中添加tr和td来实现 tab.i ...
随机推荐
- CentOS 6.7操作系统安装
如果由于是显卡驱动不兼容的话,在选择安装界面按tab键,进入命令行,然后在命令行后加上 nodmraid 关键字回车开始安装. 接下来选择hard driver 选择最后一个分区进行系统安装,然后 ...
- HD-ACM算法专攻系列(20)——七夕节
问题描述: AC源码: /**/ #include"iostream" #include"cmath" using namespace std; int mai ...
- Hua Wei 机试题目三---2014
一.根据对应规则进行翻译输出 描述:已知有如下的对应规则: ,则输入任意个正整数,输出经过规则翻译以后对应的结果. 例如:输入:1234:输出bcde. 题目很简单,我觉得需要注意的问题就是对于大整数 ...
- .NET MVC权限设计思考之切入点
在WebForm下我们一般会设计个PageBase继承Page,在OnInit方法中实现对基本权限的验证业务,然后所有的页面在继承PageBase直接继承这项基本权验证业务.而在.NET MVC下我们 ...
- 反射另一个app中的View
FrameLayout fl = (FrameLayout) findViewById(R.id.content); View v = null; try { Context context = cr ...
- JS判断客户端是否是iOS或者Android或者ipad(一)
通过判断浏览器的userAgent,用正则来判断是否是ios和Android客户端.代码如下<script type="text/javascript"> var u ...
- Pyhton学习——Day39
# CSS的常用属性# 1 颜色属性# <div style="color:rgb(255,0,0)">ppppp</div># 2 字体属性# font- ...
- springMVC传递对象参数
初学java,由于项目紧急,来不及仔细的研究,在传递参数时就老老实实的一个一个的采用@RequestParam注解方式传递,最近认真看了一下,发现java也具有类似Asp.net Mvc传递对象做参数 ...
- 小程序全局状态管理,在页面中获取globalData和使用globalSetData
GitHub: https://github.com/WozHuang/mp-extend 主要目标 微信小程序官方没有提供类似vuex.redux全局状态管理的解决方案,但是在一个完整的项目中各组件 ...
- [剑指offer] 14. 链表中倒数第K个节点+翻转+逆序打印+合并两个排序链表 + 链表相交(第一个公共节点) (链表)
题目描述 输入一个链表,输出该链表中倒数第k个结点. 思路: 两个指针,起始位置都是从链表头开始,第一个比第二个先走K个节点,当第一个走到链表尾时,第二个指针的位置就是倒数第k个节点.(两指针始终相 ...