[英]Getting from html form to javascript variable
I'm new to programming.我是编程新手。 I'm trying to build a program so when a user submits 2 integers into the form, it outputs the sum.
我正在尝试构建一个程序,以便当用户向表单提交 2 个整数时,它会输出总和。
Here's what I got so far这是我到目前为止所得到的
<html>
<h1> The Adder </h1>
<form>
Enter your first number<input type="text" id='a'> <br>
Enter your second number<input type="text"id='b' <br>
<input type= "button" onclick='add()' value="Submit"/>
</form>
<script>
function add(){
var avalue= document.getElementById('a');
var bvalue= document.getElementById('b');
alert(avalue+bvalue);
}
</script>
</html>
You need to use value
to get the value of the input:您需要使用
value
来获取输入的值:
var avalue= document.getElementById('a').value;
And the value will be a string, so you'll need parseInt
to convert it to integer:该值将是一个字符串,因此您需要
parseInt
将其转换为整数:
var avalue= parseInt(document.getElementById('a').value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.