[英]JavaScript html forms calculator not working
im trying to make a simple perimeter calculator for a projector screen. 我试图为投影仪屏幕制作一个简单的周长计算器。
The code should take the response from a radio button input and the diagonal length to calculate the perimeter accordingly. 代码应从单选按钮输入和对角线长度中获取响应,以相应地计算周长。
here is my code atm: 这是我的代码atm:
<html>
<body>
<script language="javascript">
function calc() {
var form = document.forms.Calculator;
var A = Number(getSelectedValue(form.elements.A));
var B = Number(getSelectedValue(form.elements.B));
if (A = 0) {
L = 0.8;
H = 0.6;
} else if (A = 1) {
L = 0.872;
H = 0.49;
} else {
L = 0.922;
H = 0.386;
}
form.elements.Total.value = 2 * B * (L + H);
}
function getSelectedValue(flds) {
var i = 0;
var len = flds.length;
while (i < len) {
if (flds[i].checked) {
return flds[i].value;
}
i++;
}
return "";
}
</script>
<title>Calculator</title> <pre>
<form name="Calculator">
<label>A</label>
4:3: <input name="A" type="radio" onChange="calc()" value="0" checked>
16:9: <input name="A" type="radio" onChange="calc()" value="1">
2.39:1: <input name="A" type="radio" onChange="calc()" value="2">
<label>B</label>
Screen Size: <input name="B" type="text" onChange="calc()" checked>
<label>Total</label>
<input type="text" name="Total" onChange="calc()" readonly size="10"><br>
<input type="submit" value="Submit Calculation"> <input type="reset" value="Clear">
</form>
</pre>
</body>
</html>
Your function getSelectedValue
is wrong. 您的函数getSelectedValue
错误。 It loops trough an array of elements to see which one is checked. 它循环遍历一组元素以查看已检查的元素。 You are trying to do the same for B
which is only one element so that is not going trough the loop and thus returns ''
. 您正在尝试对B
进行相同操作, B
仅是一个元素,因此不会经过循环并因此返回''
。
So instead of 所以代替
var B = Number(getSelectedValue(form.elements.B));
try 尝试
var B = Number(form.elements.B.value);
Also, your if else statements are wrong. 另外,您的if else陈述是错误的。 It should be ==
instead of =
: 应该是==
而不是=
:
if (A == 0) {
L = 0.8;
H = 0.6;
} else if (A == 1) {
L = 0.872;
H = 0.49;
} else {
L = 0.922;
H = 0.386;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.