亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在輸入文本字段 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 關注
  • 229 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號