[英]Compute value entered in textbox and place in another textbox?
I have a form with two text boxes. 我有一个带有两个文本框的表单。 I want to enter a number on one box, press the Convert link, and have it compute the number in the first box to Celsius and place it in the second text box.
我想在一个框上输入一个数字,按转换链接,让它在第一个框中将数字计算为摄氏,然后将其放在第二个文本框中。 So far it isn't working and I feel like there's something small I'm missing... What can I do?
到目前为止,它不起作用,我觉得我缺少一些小东西...该怎么办?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body
{
background-color: white;
}
h2
{
position: absolute;
left:200px;
top:5px;
color: cadetblue;
}
p
{
text-align: center;
font-family: times new roman;
color: black;
}
</style>
<script>
function f2cCalc(input)
{
var fbox = document.getElementById(input);
var cbox = document.getElementById(cinput);
var f = parseInt(fbox.value);
var cel = Math.round(5/9*(f-32));
document.getElementById(cbox).value=cel;
}
</script>
</head>
<body>
<form method="get" action="">
<h2>Temp calculator</h2>
<br />
<br />
<br/>
<br/>
<br/>
F<input id="finput" type="number" STYLE="background-color: cadetblue; color: green;" value="0"/>
C<input id="cinput" type="number" STYLE="background-color: cadetblue; color: green;" value="0"/>
<a href="javascript:f2cCalc(finput)">Convert</a>
</form>
</body>
</html>
enter code here
These two lines might be causing your problem: 这两行可能是导致您出现问题的原因:
var fbox = document.getElementById(input);
var cbox = document.getElementById(cinput);
Change to: 改成:
var fbox = document.getElementById(input);
var cbox = document.getElementById('cinput');
Basically JavaScript is trying to load the id from a variable instead of the DOM. 基本上,JavaScript尝试从变量而不是DOM加载ID。
You have typo in your code. 您的代码中有错字。 Belwo mentioned three lines are the problem.
Belwo提到了三行问题。
Change 更改
var fbox = document.getElementById(input);
var cbox = document.getElementById(cinput);
document.getElementById(cbox).value=cel;
to 至
var fbox = document.getElementById('finput');
var cbox = document.getElementById('cinput');
cbox.value=cel;
Or you can get the sweetest thing since sliced bread; 或者您可以得到自切片面包以来最甜的东西。 Jquery.
jQuery。 Insert this bad boy into your head tag:
将这个坏男孩插入您的头部标签:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
In fact, I have a fiddle for you. 实际上,我为您准备了小提琴 。 Jquery makes things a whole lot easier.
jQuery使事情变得容易得多。 Hope this works for you.
希望这对您有用。
您的函数调用应类似于:
<a href="javascript:f2cCalc('finput')">Convert</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.