js动态生成html表格,JS动态添加table表格元素

//   Last   updated   2006-02-21

function   addRowToTable()

{

var   tbl   =   document.getElementById('tblSample');

var   lastRow   =   tbl.rows.length;

//   if   there's   no   header   row   in   the   table,   then   iteration   =   lastRow   +   1

var   iteration   =   lastRow;

var   row   =   tbl.insertRow(lastRow);

//   left   cell

var   cellLeft   =   row.insertCell(0);

var   textNode   =   document.createTextNode(iteration);

cellLeft.appendChild(textNode);

//   right   cell

var   cellRight   =   row.insertCell(1);

var   el   =   document.createElement('input');

el.type   =   'text';

el.name   =   'txtRow'   +   iteration;

el.id   =   'txtRow'   +   iteration;

el.size   =   40;

el.onkeypress   =   keyPressTest;

cellRight.appendChild(el);

//   select   cell

var   cellRightSel   =   row.insertCell(2);

var   sel   =   document.createElement('select');

sel.name   =   'selRow'   +   iteration;

sel.options[0]   =   new   Option('text   zero',   'value0');

sel.options[1]   =   new   Option('text   one',   'value1');

cellRightSel.appendChild(sel);

}

function   keyPressTest(e,   obj)

{

var   validateChkb   =   document.getElementById('chkValidateOnKeyPress');

if   (validateChkb.checked)   {

var   displayObj   =   document.getElementById('spanOutput');

var   key;

if(window.event)   {

key   =   window.event.keyCode;

}

else   if(e.which)   {

key   =   e.which;

}

var   objId;

if   (obj   !=   null)   {

objId   =   obj.id;

}   else   {

objId   =   this.id;

}

displayObj.innerHTML   =   objId   +   '   :   '   +   String.fromCharCode(key);

}

}

function   removeRowFromTable()

{

var   tbl   =   document.getElementById('tblSample');

var   lastRow   =   tbl.rows.length;

if   (lastRow   >   2)   tbl.deleteRow(lastRow   -   1);

}

function   openInNewWindow(frm)

{

//   open   a   blank   window

var   aWindow   =   window.open('',   'TableAddRowNewWindow',

'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');

//   set   the   target   to   the   blank   window

frm.target   =   'TableAddRowNewWindow';

//   submit

frm.submit();

}

function   validateRow(frm)

{

var   chkb   =   document.getElementById('chkValidate');

if   (chkb.checked)   {

var   tbl   =   document.getElementById('tblSample');

var   lastRow   =   tbl.rows.length   -   1;

var   i;

for   (i=1;   i<=lastRow;   i++)   {

var   aRow   =   document.getElementById('txtRow'   +   i);

if   (aRow.value.length   <=   0)   {

alert('Row   '   +   i   +   '   is   empty');

return;

}

}

}

openInNewWindow(frm);

}

  Validate   Submit

  Display   OnKeyPress

Sample   table
1

id="txtRow1"   size="40"   οnkeypress="keyPressTest(event,   this);"   />

text   zero

text   one

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值