创建动态表字段

时间:2018-09-28 04:22:46

标签: javascript

我从提示符下的javascript创建表。但是我需要2个输入字段行/列,并且需要根据输入上的设置值来生成表

您可以看到下面的代码,我已经尝试使用javascript提示符了。

    var rows = prompt("How many rows for your multiplication table?");
    var cols = prompt("How many columns for your multiplication table?");
    if(rows == "" || rows == null)
   		 rows = 10;
    if(cols== "" || cols== null)
   		 cols = 10;
    createTable(rows, cols); // function initialize
    function createTable(rows, cols)
    {
      var j=1;
      var output = "<table border='1' width='500' cellspacing='0'cellpadding='5'>"; // to create table element
      for(i=1;i<=rows;i++) // loop for generate rows
      {
    	output = output + "<tr>";
        while(j<=cols)
        {
  		  output = output + "<td>" + i*j + "</td>"; // to create cells
   		  j = j+1;
   		}
   		 output = output + "</tr>";
   		 j = 1;
    }
    output = output + "</table>";
    document.write(output);
    }
  

2 个答案:

答案 0 :(得分:0)

如果要通过input更新表,只需将prompt替换为输入标签值并生成乘法表。您是否正在寻找下面的代码段?

function maketable(){
  var rows =document.getElementById("row").value;
  var cols = document.getElementById("col").value;
  if(rows == "" || rows == null)
    rows = 10;
  if(cols== "" || cols== null)
    cols = 10;
  createTable(rows, cols); // function initialize
  function createTable(rows, cols)
  {
    var j=1;
    var output = "<table border='1' width='500' cellspacing='0'cellpadding='5'>"; // to create table element
    for(i=1;i<=rows;i++) // loop for generate rows
    {
      output = output + "<tr>";
      while(j<=cols)
      {
        output = output + "<td>" + i*j + "</td>"; // to create cells
        j = j+1;
      }
      output = output + "</tr>";
      j = 1;
    }
    output = output + "</table>";
    document.getElementById("table-data").innerHTML = output;
  }
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>JS Bin</title>
  </head>
  <body>
    Row: <input type="number" id="row" class="row-number"></input>
  Column: <input type="number" id="col" class="row-column"></input>
  <button onclick="maketable()">Generate</button>
  <br><br>
  <div id="table-data">


  </div>
</body>
</html>

答案 1 :(得分:0)

此处是用于通过用户输入生成动态表的代码。代码中的注释进一步描述了

function generate() {

  //remove current table
  var tbl = document.getElementById("myTable");
  if (tbl)
    tbl.parentNode.removeChild(tbl);

  var rows = document.getElementById('rows').value;
  var cols = document.getElementById('cols').value;

  if (rows == "" || rows == null)
    rows = 10;
  else
    rows = +rows; //convert to integer

  if (cols == "" || cols == null)
    cols = 10;
  else
    cols = +cols; //convert to integer

  //add a table element
  var table = document.createElement("TABLE");
  table.setAttribute("id", "myTable");
  table.setAttribute("border", "1");
  table.setAttribute("width", "500");
  table.setAttribute("cellspacing", "0");
  table.setAttribute("cellpadding", "5");
  document.body.appendChild(table);

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

    //create a table row    
    var row = table.insertRow(i);

    for (var j = 0; j < cols; j++) {
      //create cell
      var cell = row.insertCell(j);
      cell.innerHTML = 'cell ' + i + ' ' + j;
    }

  }

}
<html>

<body>
  No of rows: <input type="text" id="rows"> <br> No of Columns: <input type="text" id="cols"> <br>
  <button id="generate-table" onClick="generate()">Generate</button>
</body>

</html>