Javascript 调试 - 脚本使用硬编码变量,而不是使用 getElementById('id').value
我正在尝试调试我编写的一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否正在尝试添加文本框中的数字?由于 JavaScript 变量类型系统的工作方式(结合
+
运算符的重载),2 + 2 === 4
(添加数字)但是' 2' + '2' === '22'
(字符串连接)。尝试将这些行更改为,例如:或者:
这将确保 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:or alternatively:
This will ensure that JavaScript treats the numbers as numbers rather than as strings.
JavaScript 参数的调用方式与调用 HTML 元素的方式不同。为了调用,
您需要将“roof_margin”分配给 HTML 表单元素。
JavaScript parameters can't be called in the same way that you're calling HTML elements. In order to call
you need to assign 'roof_margin' to an HTML form element.
也许你有多个具有相同 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.
确保您的代码位于 onload 函数中。否则元素可能尚未加载到 DOM 中。
Make sure your code is in an onload function. Otherwise the elements may not have been loaded into the DOM yet.