Javascript 调试 - 脚本使用硬编码变量,而不是使用 getElementById('id').value

发布于 2024-10-02 08:19:17 字数 801 浏览 4 评论 0原文

我正在尝试调试我编写的一些 JavaScript,但无法弄清楚为什么它不起作用。如果我对变量进行硬编码,它可以正常工作,但如果我使用 document.getElementById('id').value 来获取变量,它就会失败。

下面的示例工作正常,但一旦我取消注释注释行,它就不起作用了。在第二部分之前和之后打印变量,它们似乎是相同的。

实在不明白是怎么回事。也许我只是需要睡觉,但如果有人有建议那就太好了!

roof_width = 5;
roof_depth = 3;
panel_width = 2;
panel_depth = 1;
panel_power = 200;
roof_margin = 0.100;
panel_gap = 0.05;

roof_width = document.getElementById('roof_width').value;
roof_depth = document.getElementById('roof_depth').value;
// panel_width = document.getElementById('panel_width').value;
// panel_depth = document.getElementById('panel_depth').value;
panel_power = document.getElementById('panel_power').value;
// roof_margin = document.getElementById('roof_margin').value;
panel_gap = document.getElementById('panel_gap').value;

I'm trying to debug some javascript I wrote and can't figure out why it's not working. If I hard code the variables it works fine, but if I use document.getElementById('id').value to get the variable it fails.

The example below works fine but as soon as I un-comment the commented lines it doesn't. Printing the variables before and after the second section they seem to be identical.

Really don't get what's going on. Maybe I just need to sleep on it, but if anyone's got suggestions that would be great!

roof_width = 5;
roof_depth = 3;
panel_width = 2;
panel_depth = 1;
panel_power = 200;
roof_margin = 0.100;
panel_gap = 0.05;

roof_width = document.getElementById('roof_width').value;
roof_depth = document.getElementById('roof_depth').value;
// panel_width = document.getElementById('panel_width').value;
// panel_depth = document.getElementById('panel_depth').value;
panel_power = document.getElementById('panel_power').value;
// roof_margin = document.getElementById('roof_margin').value;
panel_gap = document.getElementById('panel_gap').value;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人间不值得 2024-10-09 08:19:17

您是否正在尝试添加文本框中的数字?由于 JavaScript 变量类型系统的工作方式(结合 + 运算符的重载),2 + 2 === 4(添加数字)但是 ' 2' + '2' === '22' (字符串连接)。尝试将这些行更改为,例如:

panel_width = parseFloat(document.getElementById('panel_width').value);

或者:

panel_width = Number(document.getElementById('panel_width').value);

这将确保 JavaScript 将数字视为数字而不是字符串。

Are you trying to add numbers that are in text boxes? Because of the way JavaScript's variable typing system works (combined with the overloading of the + operator), 2 + 2 === 4 (adding numbers) but '2' + '2' === '22' (string concatenation). Try changing the lines to, for example:

panel_width = parseFloat(document.getElementById('panel_width').value);

or alternatively:

panel_width = Number(document.getElementById('panel_width').value);

This will ensure that JavaScript treats the numbers as numbers rather than as strings.

污味仙女 2024-10-09 08:19:17

JavaScript 参数的调用方式与调用 HTML 元素的方式不同。为了调用,

document.getElementById('roof_margin').value;

您需要将“roof_margin”分配给 HTML 表单元素。

JavaScript parameters can't be called in the same way that you're calling HTML elements. In order to call

document.getElementById('roof_margin').value;

you need to assign 'roof_margin' to an HTML form element.

等数载,海棠开 2024-10-09 08:19:17

也许你有多个具有相同 id 的 dom 元素?请记住 dom 元素 ID 必须是唯一的。我建议你使用 jquery 来使 javascript 与 html 交互。

Pherhaps you have multiple dom elements with the same id? Remember the dom element ID must be unique. I suggest you to use jquery for interacting javascript with html.

手心的温暖 2024-10-09 08:19:17

确保您的代码位于 onload 函数中。否则元素可能尚未加载到 DOM 中。

window.onload = funciton(){/* code here */};

Make sure your code is in an onload function. Otherwise the elements may not have been loaded into the DOM yet.

window.onload = funciton(){/* code here */};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文