未捕获类型错误:无法读取 null 的属性(读取“值”)getElementById

问题描述 投票:0回答:1

单击按钮时出现此错误。

未捕获的类型错误:无法读取 null 的属性(读取“值”)

我做错了什么?这是代码:

 <table>
                <tr>
                    <th>Height (inches)</th>
                    <th>Width (inches)</th>
                    <th>Length (inches)</th>
                    <th>Pieces</th>
                    <th>Bd Ft</th>
                    <th>Lineal Ft</th>
                    <th>Sqare Ft</th>
                </tr>
                <tr>
                    <td>
                        <input type="text" value="1" id="height" /></td>
                    <td>
                        <input type="text" value="1" id="width" /></td>
                    <td>
                        <input type="text" value="1" id="length" /></td>
                    <td>
                        <input type="text" value="1" id="pieces" /></td>
                    <td>
                        <input type="text" value="" id="bdft" /></td>
                    <td>
                        <input type="text" value="" id="linealft" /></td>
                    <td>
                        <input type="text" value="" id="sqft" /></td>
                </tr>
            </table>
            <input type="button" value="Calculate" onclick="calculate()" />
            <script type="text/javascript">
                function calculate() {
                   
                    var heightVar = document.getElementById(height);
                    var widthVar = document.getElementById(width);
                    var lengthVar = document.getElementById(length);
                    var piecesVar = document.getElementById(pieces);

                    alert(document.getElementById(height).value);
                  
                }

            </script>

我做错了什么?我希望在警报窗口中获得“高度”值。

javascript getelementbyid
1个回答
0
投票

我缺少引文(废话)。

var heightVar = document.getElementById(height);

需要

var heightVar = document.getElementById("高度");

© www.soinside.com 2019 - 2024. All rights reserved.