如何在html表中添加数字

时间:2021-05-31 19:39:53

I'm trying to add a column of numbers in an HTML table. I don't receive an errors in the console, I simply do not get a sum. here is a snippet of the HTML, and the JS in question. As you can see each cell in the column has its own class, mile1, mile2, etc... There are 48 of these in the full HTML. The script runs without any problems and does what it should do, it just doesn't give a sum of the column "Miles".

我正在尝试在HTML表格中添加一列数字。我没有收到控制台中的错误,我只是没有得到一笔钱。这是HTML的片段,以及有问题的JS。正如您所看到的,列中的每个单元格都有自己的类,mile1,mile2等...完整的HTML中有48个。该脚本运行没有任何问题,并做它应该做的事情,它只是没有给出“Miles”列的总和。

<body>
    <div id="map" style="height:400px"></div>
    <div id="status"></div>
    <div id="results"></div>
    <div id="table"></div>
    <div id="td"</div>

    <div style=" text-align: center; text-indent: 0px; padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;">

        <table width="20%" border="1" cellpadding="0" cellspacing="0" "border-color: #000000; border-style: solid; background-color: #ffffff;">
            <tr>
                <th>State</th>
                   <th>Miles</th>       

                <tr valign="top">
                <td style="border-color : #000000 #000000 #000000 #000000; border-style: solid;"class="state1"><br />
                </td>
                <td style="border-color : #000000 #000000 #000000 #000000; border-style: solid;"class="mile1"><br />
                </td>
                <tr valign="top">
                    <td style="border-color : #000000 #000000 #000000 #000000; border-style: solid;" class="state2"><br />
                    </td>
                    <td style="border-color : #000000 #000000 #000000 #000000; border-style: solid;" class="mile2"><br />
                    </td>
                    <tr valign="top">





                       var x =(stateMiles[state]);

                     (stateMiles[state])= parseFloat(x);    

                     var m = (stateMiles[state]);
                     var n = Number(m);
                     var r = Math.round(m);
                     (stateMiles[state]) = Number(m);
                     (stateMiles[state]) = Math.round(m);   



                        $("#results").append;
                        $(".state" + i).append(state);
                        $(".mile" + i).append(stateMiles[state]);

//here is what i have been trying //

//这是我一直在尝试的//

        var numbers = [(stateMiles[state])];                                }

        function getSum(total, num) {
    return total + Math.round(num);
    document.getElementById("table").td(mile1,mile2) = numbers.reduce(getSum,0);

1 个解决方案

#1


0  

Sadly neither your code or your JSFiddle is a small / minimal working example.

遗憾的是,您的代码或JSFiddle都不是一个小/最小的工作示例。

Generally speaking you should be able to do what you want as long as you watch for the two following pitfalls:

一般来说,只要你注意以下两个陷阱,你应该能够做你想做的事情:

  1. A table has a tbody child-element (so a row is not a direct child of a table)
  2. 表有一个tbody子元素(因此一行不是表的直接子元素)

  3. You might need to parse the content of a Cell into a number
  4. 您可能需要将Cell的内容解析为数字

Considering this, the following snippet gets a value from a table. Making it a sum of those should be done by simply running over the table domNode "array"

考虑到这一点,以下代码段从表中获取值。将它们作为一个总和应该通过简单地遍历表domNode“数组”来完成

<table id="table">
    <tr><td>1</td><td>2</td></tr>
    <tr><td>3</td><td>4</td></tr>
</table>

To get the first value you need:

要获得您需要的第一个值:

parseInt(document.getElementById("table").children[0].children[0].children[0].textContent)

The children chaining is table->tbody->tr->td

链接的孩子是table-> tbody-> tr-> td

#1


0  

Sadly neither your code or your JSFiddle is a small / minimal working example.

遗憾的是,您的代码或JSFiddle都不是一个小/最小的工作示例。

Generally speaking you should be able to do what you want as long as you watch for the two following pitfalls:

一般来说,只要你注意以下两个陷阱,你应该能够做你想做的事情:

  1. A table has a tbody child-element (so a row is not a direct child of a table)
  2. 表有一个tbody子元素(因此一行不是表的直接子元素)

  3. You might need to parse the content of a Cell into a number
  4. 您可能需要将Cell的内容解析为数字

Considering this, the following snippet gets a value from a table. Making it a sum of those should be done by simply running over the table domNode "array"

考虑到这一点,以下代码段从表中获取值。将它们作为一个总和应该通过简单地遍历表domNode“数组”来完成

<table id="table">
    <tr><td>1</td><td>2</td></tr>
    <tr><td>3</td><td>4</td></tr>
</table>

To get the first value you need:

要获得您需要的第一个值:

parseInt(document.getElementById("table").children[0].children[0].children[0].textContent)

The children chaining is table->tbody->tr->td

链接的孩子是table-> tbody-> tr-> td