Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中垂直填充数据单元?_Javascript_Html - Fatal编程技术网

如何在Javascript中垂直填充数据单元?

如何在Javascript中垂直填充数据单元?,javascript,html,Javascript,Html,我有一个HTML格式的(名称、值)表,我想在一些计算之后填充“value”列 “名称”列在加载网页时填写,而“值”列在加载网页时保持空白 在执行一些计算之后,我想为与“name”对应的每一行依次填充“value”列。从1->n 我的页面的作用: 从用户那里获取一个数字(比如3) 得到3个名字 打印出一张由3行对组成的表格,将“值”列留空 执行一些计算并将它们存储在名为“value”的数组中 下一步是什么: 我想为存储在“value”数组中的每个元素依次填充“value”列,即从1->n. 下

我有一个HTML格式的(名称、值)表,我想在一些计算之后填充“value”列

“名称”列在加载网页时填写,而“值”列在加载网页时保持空白

在执行一些计算之后,我想为与“name”对应的每一行依次填充“value”列。从1->n

我的页面的作用:

  • 从用户那里获取一个数字(比如3)
  • 得到3个名字
  • 打印出一张由3行对组成的表格,将“值”列留空
  • 执行一些计算并将它们存储在名为“value”的数组中
  • 下一步是什么:


    我想为存储在“value”数组中的每个元素依次填充“value”列,即从1->n.


    下面是一些代码:

    JavaScript-根据开始时收到的数字动态创建表

    var playerTable = document.getElementById("player_table");
    var player_table_row, player_table_datacell1, player_table_datacell2;
    var value;
    
        //no_of_players contains the number of rows
        for ( var i = 0; i < no_of_players; i++)
        {
            //inserts i'th row
            player_table_row = playerTable.insertRow(i);
    
            //Insert first datacell for the row
            player_table_datacell1 = player_table_row.insertCell( 0 );
            //Insert second datacell for the row
            player_table_datacell2 = player_table_row.insertCell( 1 );
    
            //Inserts the name for the i'th row in the 'name' column
            //players_list is the array containing all the names
            player_table_datacell1.innerHTML = players_list[i];
            //Leave the second cell i.e. 'value' column blank
            player_table_datacell2.innerHTML = "";
    
    
            value[i] = /* Some computations */
        }
    
    我已经提前计算了这些值5、2、19,现在只需要在HTML表中填充它们。我当前的表如下所示:

    Name    Value
    A       
    B       
    C       
    
    <table id = "player_table">
      <tr>
        <th>Name</th>
        <th>Value</th>
      </tr>
      <tr>
        <td>A</td>
        <td></td>
      </tr>
      <tr>
        <td>B</td>
        <td></td>
      </tr>
      <tr>
        <td>C</td>
        <td></td>
      </tr>
    </table>
    

    我假设您的计算需要某种用户输入。这就是为什么不能进行计算,然后向表中添加包含名称和计算值的行

    这里有一种方法。我只是为每个值单元格设置id,并使用id进行更新。希望有帮助:)

    
    名称
    价值
    过程();
    函数过程(){
    var playerTable=document.getElementById(“player_table”);
    变量player\u table\u row、player\u table\u datacell1、player\u table\u datacell2;
    var值;
    //用于测试目的的硬编码
    没有球员的人数=3人;
    var players_list=[“A”、“B”、“C”];
    //no_of_player包含行数
    对于(变量i=0;i
    生成的表格如下:

    Name    Value
    A       
    B       
    C       
    
    <table id = "player_table">
      <tr>
        <th>Name</th>
        <th>Value</th>
      </tr>
      <tr>
        <td>A</td>
        <td></td>
      </tr>
      <tr>
        <td>B</td>
        <td></td>
      </tr>
      <tr>
        <td>C</td>
        <td></td>
      </tr>
    </table>
    
    然后在循环中的每个单元格上设置innerHTML

    valueCells[i].innerHTML = value[i];
    

    你能公布你的预期产出吗?你面临的问题是什么?你似乎知道如何填好表格。有什么问题吗?与文本所说的相反,表的HTML代码只有两个单元格,甚至在语法上也有错误。那么,表中是否包含名称(如何?),或者是否包含名称?@Yasin我已经根据我的输出要求更新了我的问题。@JukkaK.Korpela每行只有2个单元格。行数的变化取决于提供给变量“no_of_players”的输入,该变量早就从用户提示中获取了。是的,这很有效。我对代码进行了调整,以动态地包含“player\u table\u datacell2”变量的“id”值(我不知道)。还有,我要澄清的是,我的价值观已经准备好了。我已经计算过了。它们实际上并没有以任何方式链接到HTML。我唯一的问题是如何显示它们。
    var valueCells = document.querySelectorAll("#player_table tr td:nth-child(2)");
    
    valueCells[i].innerHTML = value[i];