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

如何在下面的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/


慕标5832272
浏览 56回答 1
1回答

九州编程

我刚刚使用了一个counter变量,这就是你需要的?var counter = 0;function generate_table() {&nbsp; counter++;&nbsp; // get the reference for the body&nbsp; var body = document.getElementsByTagName('body')[0];&nbsp; // creates a <table> element and a <tbody> element&nbsp; var tbl = document.createElement('table');&nbsp; var tblBody = document.createElement('tbody');&nbsp; // creating all cells&nbsp; for (var i = 0; i < 1; i++) {&nbsp; &nbsp; var seqnumber = 1;&nbsp; &nbsp; var seq = +1;&nbsp; &nbsp; // creates a table row&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var row2 = document.createElement('tr');&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; //====== table first row data =======//&nbsp; &nbsp; var seq = document.createElement('td');&nbsp; &nbsp; var seqText = document.createTextNode('Seq');&nbsp; &nbsp; var l = document.createElement('td');&nbsp; &nbsp; var seqText1 = document.createElement('input');&nbsp; &nbsp; seqText1.value = counter;&nbsp; &nbsp;&nbsp; &nbsp; //===== seq generator =====//&nbsp; &nbsp; seq.appendChild(seqText);&nbsp; &nbsp; row2.appendChild(seq);&nbsp; &nbsp; l.appendChild(seqText1);&nbsp; &nbsp; row2.appendChild(l);&nbsp; &nbsp;&nbsp; &nbsp; // add the row to the end of the table body&nbsp; &nbsp; tblBody.appendChild(row2);&nbsp;&nbsp;&nbsp; }&nbsp; // put the <tbody> in the <table>&nbsp; tbl.appendChild(tblBody);&nbsp; // appends <table> into <body>&nbsp; body.appendChild(tbl);&nbsp; // sets the border attribute of tbl to 2;&nbsp; tbl.setAttribute('border', '2');}<input type="button" value="Generate a table." onclick="generate_table()" />
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5