小编给大家分享一下JS/jQuery如何实现超简单的Table表格添加,删除行功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
本文实例讲述了JS/jQuery实现超简单的Table表格添加,删除行功能。分享给大家供大家参考,具体如下:
<html>
<head>
<title>table添加/删除行</title>
</head>
<body>
<table id="testTbl" border=1>
<tr>
<td>性别</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tr>
<td>
<select name="a">
<option value="男">男</option>
<option value="女">女</option>
</select>
</td>
<td>
<input type="text" name="b">
</td>
<td>
<input type="text" name="c">
</td>
</tr>
</table>
<input type="button" name="Submit2" value="添加" onclick="addRow()">
<script>
function addRow(){
//添加行
var newTr = testTbl.insertRow();
//添加列
var newTd0 = newTr.insertCell();
var newTd1 = newTr.insertCell();
var newTd2 = newTr.insertCell();
var newTd3 = newTr.insertCell();
//设置列内容和属性
newTd0.innerText = document.all("a").options[document.all("a").selectedIndex].text;
newTd1.innerText = document.all("b").value;
newTd2.innerText = document.all("c").value;
newTd3.innerHTML= '<input type="button" name="del" value="删除" οnclick="del(this)">';
}
function del(o){
//获取表格
var t=document.getElementById('testTbl');
//删除当前行
t.deleteRow(o.parentNode.parentNode.rowIndex)
}
</script>
</body>
</html>
//动态添加行
function addRow(){
var table = document.getElementById("tableID");
var newRow = table.insertRow(); //创建新行
var newCell1 = newRow.insertCell(); //创建新单元格
newCell.innerHTML = ""; //单元格内的内容
newCell.setAttribute("align","center"); //设置位置
}
//动态删除行
function deleteRow(){
var rowIndex = event.srcElement.parentElement.parentElement.rowIndex;
var styles = document.getElementById("tableID");
styles.deleteRow(rowIndex);
}
用克隆的方式
<html>
<head>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
var index = 0;
$(document).ready(function(){
$("button").click(function(){
index++;
$("body").append($("p").clone().attr({'id':'name'+index,'name':'pwd'+index}));
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>复制每个 p 元素,然后追加到 body 元素</button>
</body>
</html>
下面是直接添加和删除当前table表格,很好用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>" rel="external nofollow" >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<a href = "javascript:void(0);" οnclick="a();">添加</a>
<a href = "javascript:void(0);" οnclick="b();">显示</a>
<table id = "tab"></table>
</body>
<script type="text/javascript">
var index = 0;
var a = function(){
index++;
var tab = "<table id = 'tab_"+index+"' border = '1' >";
tab += "<tr><td colspan = '6' align = 'center'>尺寸规格</td></tr>";
tab += "<tr><td>长度</td><td><input οnblur='b("+index+");' id = 'cd_"+index+"' /></td><td align = 'right'>宽度</td><td><input οnblur='b("+index+");' id = 'kd_"+index+"' /></td><td>数量(个)</td><td><input οnblur='c("+index+");' id = 'sl_"+index+"' /></td></tr>";
tab += "<tr><td>摆放区域</td><td><input id = 'bfqy_"+index+"' /></td><td>单个面积(平方米)</td><td><input readonly = 'readonly' id = 'dgmj_"+index+"' /></td><td>总面积</td><td><input readonly = 'readonly' id = 'zmj_"+index+"' /></td></tr>";
tab += "<tr><td>内容描述</td><td colspan = '4'><textarea rows='3' cols='70' id = 'content_"+index+"' ></textarea></td><td><input type = 'button' value = '删除' onclick = 'del("+index+");' id = 'del_"+index+"' /></td></tr>";
tab += "</table>";
$('#tab').append(tab);
}
function del(ind){
$('#tab_'+ind).remove();
}
function b(ind){
var cdd = $('#cd_'+ind).val();
var kdd = $('#kd_'+ind).val();
if(cdd==''){cdd = 1;}
if(kdd==''){kdd = 1;}
if(cdd==''&&kdd==''){
$('#dgmj_'+ind).val('');
}else{
$('#dgmj_'+ind).val(cdd * kdd);
}
}
function c(ind){
var cdd = $('#cd_'+ind).val();
var kdd = $('#kd_'+ind).val();
var sll = $('#sl_'+ind).val();
if(cdd==''){cdd = 1;}
if(kdd==''){kdd = 1;}
if(sll==''){sll = 1;}
if(cdd==''&&kdd==''&&sll==''){
$('#zmj_'+ind).val('');
}else if(cdd!=''&&kdd!=''&&sll!=''){
$('#zmj_'+ind).val(cdd * kdd * sll);
}else{
$('#zmj_'+ind).val('');
}
}
</script>
</html>
JS是JavaScript的简称,它是一种直译式的脚本语言,其解释器被称为JavaScript引擎,是浏览器的一部分,主要用于web的开发,可以给网站添加各种各样的动态效果,让网页更加美观。
看完了这篇文章,相信你对“JS/jQuery如何实现超简单的Table表格添加,删除行功能”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。