[英]Calculator on HTML/CSS/Javascript
Hello I have been struggling to learn HTML CSS and JAVASCRIPT.您好,我一直在努力学习 HTML CSS 和 JAVASCRIPT。 I have never tried and don't know basics exactly and I want to create a calculator and even for the basics I did simple example of x+1/2 and the answer I get is wrong here is the code
我从未尝试过,也不完全了解基础知识,我想创建一个计算器,即使是基础知识,我也做了 x+1/2 的简单示例,但我得到的答案是错误的,这里是代码
function result() { var x = document.getElementById("constant").value; var y = document.getElementById("height").value; var z = document.getElementById("freq").value; var c = 3 * 10 ** 8; var k = x + 1; var k2 = k / 2; document.getElementById("Calculate").value = k2; }
<!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>
Keep in mind that input value is always a string.请记住,输入值始终是字符串。 And if you add to a string, you get a concatenated string, ie:
如果你添加到一个字符串,你会得到一个连接的字符串,即:
'1' + 1 === '11'
Convert your input values to numbers to make them behave like... well, numbers:将您的输入值转换为数字,使它们表现得像......好吧,数字:
var x = Number(document.getElementById("constant").value);
var y = Number(document.getElementById("height").value);
var z = Number(document.getElementById("freq").value);
Inputs from a form are always strings.表单的输入始终是字符串。 To illustrate this, here is a small example:
为了说明这一点,这里有一个小例子:
a = "2" b = "3" console.log(a + b) // but after convert string to int with the JS Function Number() console.log(Number(a) + Number(b))
This means, you have to convert all input from your form to numbers first.这意味着,您必须首先将表单中的所有输入转换为数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.