【问题标题】:JS Calculator Gives `getElementById is not defined` [duplicate]JS计算器给出`getElementById未定义`[重复]
【发布时间】:2021-10-01 00:41:35
【问题描述】:

这段代码有什么问题?我已经尝试过,但无法弄清楚。我是 HTML、JavaScript 和 CSS 方面的新手。 我在互联网上闲逛,但我认为我遗漏了一些东西,这是一个小错误。你能帮忙吗?我必须为即将进行的项目学习这一点

这里是代码

function result() {
  let a = getElementById("constant").value;
  let b = getElementById("height").value;
  let c = getElementById("freq").value;
  let sum = Number(a) + Number(b) + Number(c);
  document.getElementById("Calculate").value = sum;
}
<form>
  Di-Electric Constant: <input class="text" Placeholder="Enter Value" id="constant" </input>
  <br> Di-Electric Height: <input class="text" Placeholder="Enter Value" id="height" </input>
  <br> Operational Frequency: <input class="text" Placeholder="Enter Value" id="freq" </input>
  <br>

  <br>
  <input type="text" id="Calculate" </input>
  <input type="button" value="Calculate" onclick="result()">
</form>
<br/>

【问题讨论】:

  • document.getElementById()
  • 你需要使用document.getelementById()。另外,不要忘记接受您问题的正确答案。

标签: javascript html css


【解决方案1】:

它是document.getElementById()。见:MDN WebDocs: document.getElementById().

Document 方法 getElementById() 返回一个 Element 对象,该对象表示其 id 属性与指定字符串匹配的元素。由于元素 ID 在指定时必须是唯一的,因此它们是快速访问特定元素的有用方法。

function result()
{

let a = document.getElementById("constant").value;
let b = document.getElementById("height").value;
let c = document.getElementById("freq").value;
let sum = Number(a) + Number(b) + Number(c);

document.getElementById("Calculate").value = sum;

}
<!DOCTYPE html>
<html>
<head>
<h1>Microstrip Calculator</h1>
<p>Enter the following values in numeric form:</p>
</head>
<body>
<form>

Di-Electric Constant: <input class="text"Placeholder="Enter Value" id="constant"</input>
<br>
Di-Electric Height: <input class="text"Placeholder="Enter Value" id="height"</input>
<br>
Operational Frequency: <input class="text"Placeholder="Enter Value" id="freq"</input>
<br>

<br>
<input type="text"id="Calculate"</input>
<input type="button" value="Calculate" onclick="result()"> 
</form>
<br/>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-24
    • 2017-04-29
    • 1970-01-01
    • 2022-01-22
    • 2023-03-28
    • 2021-09-06
    • 2014-07-03
    • 1970-01-01
    相关资源
    最近更新 更多