为了账号安全,请及时绑定邮箱和手机立即绑定

如何在输入文本字段 HTML 中添加序列号

如何在输入文本字段 HTML 中添加序列号

慕标5832272 2024-01-22 20:58:00
如何在下面的HTML输入文本框中添加序列号。每当用户单击“生成表”按钮时,输入框中的序列号应显示为 1、2、3 任何解决方案function generate_table() {  // get the reference for the body  var body = document.getElementsByTagName('body')[0];  // creates a <table> element and a <tbody> element  var tbl = document.createElement('table');  var tblBody = document.createElement('tbody');  // creating all cells  for (var i = 0; i < 1; i++) {    var seqnumber = 1;    var seq = +1;    // creates a table row        var row2 = document.createElement('tr');        //====== table first row data =======//    var seq = document.createElement('td');    var seqText = document.createTextNode('Seq');    var l = document.createElement('td');    var seqText1 = document.createElement('input');       //===== seq generator =====//    seq.appendChild(seqText);    row2.appendChild(seq);    l.appendChild(seqText1);    row2.appendChild(l);       // add the row to the end of the table body    tblBody.appendChild(row2);    }  // put the <tbody> in the <table>  tbl.appendChild(tblBody);  // appends <table> into <body>  body.appendChild(tbl);  // sets the border attribute of tbl to 2;  tbl.setAttribute('border', '2');}<input type="button" value="Generate a table." onclick="generate_table()" />我的jsfiddle链接:https://jsfiddle.net/shreekantbatale2/bdwLuhgs/5/
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

我刚刚使用了一个counter变量,这就是你需要的?


var counter = 0;


function generate_table() {

  counter++;


  // get the reference for the body

  var body = document.getElementsByTagName('body')[0];


  // creates a <table> element and a <tbody> element

  var tbl = document.createElement('table');

  var tblBody = document.createElement('tbody');


  // creating all cells

  for (var i = 0; i < 1; i++) {

    var seqnumber = 1;

    var seq = +1;

    // creates a table row

    

    var row2 = document.createElement('tr');


    


    //====== table first row data =======//

    var seq = document.createElement('td');

    var seqText = document.createTextNode('Seq');

    var l = document.createElement('td');

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

    seqText1.value = counter;

   


    //===== seq generator =====//

    seq.appendChild(seqText);

    row2.appendChild(seq);

    l.appendChild(seqText1);

    row2.appendChild(l);


   


    // add the row to the end of the table body

    tblBody.appendChild(row2);

  

  }


  // put the <tbody> in the <table>

  tbl.appendChild(tblBody);

  // appends <table> into <body>

  body.appendChild(tbl);

  // sets the border attribute of tbl to 2;

  tbl.setAttribute('border', '2');

}

<input type="button" value="Generate a table." onclick="generate_table()" />


查看完整回答
反对 回复 2024-01-22
  • 1 回答
  • 0 关注
  • 44 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信