JavaScript:table元素行的添加和删除

projectcostdays 
879263
 
 
 
 
source
代码
 
    
< html >

< head >

< title > js </ title >
< script language ="javascript" type ="text/javascript" >
var activeRow = 0 ;
function addRow()
{
var i;
var tb = document.getElementById( " table1 " );
var row = tb.insertRow(activeRow + 1 );

for (i = 0 ;i < 4 ;i ++ ){
var cell = row.insertCell(i);
if (i == 0 ){
cell.innerHTML
= getInputString( " text " , "" );
cell.className
= " bd " ;
}
if (i == 1 ){
cell.innerHTML
= parseInt(Math.random() * 1000 );
cell.className
= " bd " ;
}
if (i == 2 ){
cell.innerHTML
= " 30 " ;
cell.className
= " bd " ;
}
if (i == 3 ){
cell.innerHTML
= getInputString( " button " , " L " );
}
}
}
function delRow()
{
var tb = document.getElementById( " table1 " );
if (activeRow >= tb.rows.length){
activeRow
= tb.rows.length - 1 ;
}
tb.deleteRow(activeRow);
}

function getInputString(sType, sValue)
{
var str = " <input type=' " + sType + " ' value=' " + sValue + " ' id='btn' " ;
if (sType == " button " ){
str
+= " οnclick='getData();' " ;
}
str
= str + " /> " ;
return str;
}
function getData()
{
currentRow();
var tb = document.getElementById( " table1 " );
var v = parseInt(Math.random() * 151 + 50 );
tb.rows(activeRow).cells(
2 ).innerHTML = v;
}

function currentRow(){
var obj = event.srcElement;
if (obj.tagName == ' INPUT ' ){
obj
= obj.parentElement;
}
activeRow
= obj.parentElement.rowIndex;
}

</ script >
< style type ="text/css" >
.bd
{ border : #ccc solid 1px ;
margin
: 0px ;
padding
: 0px ;
font-size
: 20px ;
}

[type="button"]
{
border
: 1px outset ;
background-color
: #C0C0C0 ;
font-size
: 10pt ;
}
[type="text"]
{
border
: inset 1px #eeeeee ;
padding
: 0px ;
background-color
: Yellow ;
vertical-align
: middle ;
width
: 150px ;
height
: 30px ;
}
</ style >
</ head >

< body >
< div >
< table id ="table1" onclick ="currentRow();" >
< tr >
< td width ="100px" > project </ td >
< td width ="50px" > cost </ td >
< td width ="50px" > days </ td >
< td width ="30px" > &nbsp; </ td >
</ tr >
< tr >
< td class ="bd" >< input type ="text" id ="bRow2" value ="kundianxian" /></ td >
< td class ="bd" > 8792 </ td >
< td class ="bd" > 63 </ td >
< td >< input type ="button" value =" L " id ="btn1" /> </ td >
</ tr >
</ table >
</ div >
< hr style =" width:400px;" align ="left" />
< div >
< input type ="button" value ="row add" id ="btnAdd" name ="btnAdd" onclick ="addRow();" />
< input type ="button" value ="row delete" id ="btnDel" name ="btnDel" onclick ="delRow();" />
</ div >

</ body >

</ html >

 

转载于:https://www.cnblogs.com/cnbwang/archive/2010/11/11/1874840.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在Bootstrap Table中实现多个表单的新增和删除,可以使用Bootstrap Table的扩展插件editable。 首先,确保你已经引入了Bootstrap Table和editable插件的JS和CSS文件。然后,在表格头部添加一个新的列,用于操作新增和删除: ```html <th data-field="operate" data-formatter="operateFormatter">操作</th> ``` `operateFormatter`是一个自定义的格式化函数,它将在每数据中添加一个操作列,用于新增和删除。 接下来,在JavaScript代码中定义`operateFormatter`函数: ```javascript function operateFormatter(value, row, index) { return [ '<a class="add" href="javascript:void(0)" title="Add">', '<i class="glyphicon glyphicon-plus"></i>', '</a>', '<a class="remove" href="javascript:void(0)" title="Remove">', '<i class="glyphicon glyphicon-remove"></i>', '</a>' ].join(''); } ``` 该函数返回两个链接元素,一个用于新增,另一个用于删除。当用户点击“新增”链接时,会添加一个新的空;当用户点击“删除”链接时,会删除。 最后,在JavaScript代码中添加以下事件监听器: ```javascript $(function() { $('#table').bootstrapTable({ onEditableSave: function(field, row, oldValue, $el) { // 当表格的数据被修改时触发此事件 } }); // 新增 $(document).on('click', '.add', function() { var $table = $('#table'); $table.bootstrapTable('insertRow', { index: $table.bootstrapTable('getData').length, row: {} }); }); // 删除 $(document).on('click', '.remove', function() { var $table = $('#table'); var index = $(this).parents('tr').data('index'); $table.bootstrapTable('removeByUniqueId', index); }); }); ``` 在这段代码中,我们添加了两个事件监听器,一个用于新增,另一个用于删除。`insertRow`方法用于在表格中插入一,`removeByUniqueId`方法用于删除指定的。 注意,`onEditableSave`事件用于在编辑表格的数据时触发,你可以在其中对数据进保存操作。 希望这个示例可以帮助你实现多个表单的新增和删除

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值