Posted in jQuery onJuly 31, 2019
本文实例讲述了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>
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.3water.com/code/HtmlJsRun测试上述代码运行效果。
希望本文所述对大家JavaScript程序设计有所帮助。
JS/jQuery实现超简单的Table表格添加,删除行功能示例
- Author -
Love丶兰声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@