I have a simple HTML form, with 3 input field, when you hit = button, it suppose to add 2 numbers that you typed in the first 2 input fields and display result in 3rd filed. Is it possible to do that? (I need result in a box (or table) or some thing rather than in plain text). I try the following but doesn't work (it does nothing), can somebody help me?
我有一个简单的HTML表单,有3个输入字段,当你点击=按钮时,它会添加你在前2个输入字段中输入的2个数字,并在第3个字段中显示结果。有可能做到吗?(我需要一个方框(或表)或一些东西,而不是纯文本)。我尝试了下面的方法,但是没有效果(没有效果),有人能帮我吗?
<HTML>
<HEAD>
<TITLE>Sum</TITLE>
<script type="text/javascript">
function sum()
{
var num1 = document.myform.number1.value;
var num2 = document.myform.number2.value;
var sum = parseInt(num1) + parseInt(num2);
document.getElementById('add').innerHTML = sum;
}
</script>
</HEAD>
<BODY>
<FORM NAME="myform">
<INPUT TYPE="text" NAME="number1" VALUE=""> +
<INPUT TYPE="text" NAME="number2" VALUE="">
<INPUT TYPE="button" NAME="button" Value="=" onClick="sum()">
<INPUT TYPE="text" ID="add" NAME="result" VALUE="">
</FORM>
</BODY>
</HTML>
4 个解决方案
#1
8
innerHTML
sets the text (including html elements) inside an element. Normally we use it for elements like div, span etc to insert other html elements inside it.
innerHTML在元素中设置文本(包括html元素)。通常,我们将它用于div、span等元素,以便在其中插入其他html元素。
For your case you want to set the value of an input element. So you should use the value
attribute.
对于这种情况,您需要设置输入元素的值。所以你应该使用value属性。
Change innerHTML
to value
改变innerHTML值
document.getElementById('add').value = sum;
#2
3
Do you really want the result to come up in an input box? If not, consider a table with borders set to other than transparent and use
你真的希望结果显示在输入框中吗?如果不是,请考虑设置为透明和使用之外的边框的表
document.getElementById('sum').innerHTML = sum;
. getelementbyid(”和“)。innerHTML =总和;
#3
3
<HTML>
<HEAD>
<TITLE>Sum</TITLE>
<script type="text/javascript">
function sum()
{
var num1 = document.myform.number1.value;
var num2 = document.myform.number2.value;
var sum = parseInt(num1) + parseInt(num2);
document.getElementById('add').value = sum;
}
</script>
</HEAD>
<BODY>
<FORM NAME="myform">
<INPUT TYPE="text" NAME="number1" VALUE=""/> +
<INPUT TYPE="text" NAME="number2" VALUE=""/>
<INPUT TYPE="button" NAME="button" Value="=" onClick="sum()"/>
<INPUT TYPE="text" ID="add" NAME="result" VALUE=""/>
</FORM>
</BODY>
</HTML>
This should work properly. 1. use .value instead of "innerHTML" when setting the 3rd field (input field) 2. Close the input tags
这应该正常工作。1。在设置第3个字段(输入字段)2时,使用.value代替“innerHTML”。关闭输入标签
#4
0
With .value and INPUT tag
<HTML>
<HEAD>
<TITLE>Sum</TITLE>
<script type="text/javascript">
function sum()
{
var num1 = document.myform.number1.value;
var num2 = document.myform.number2.value;
var sum = parseInt(num1) + parseInt(num2);
document.getElementById('add').value = sum;
}
</script>
</HEAD>
<BODY>
<FORM NAME="myform">
<INPUT TYPE="text" NAME="number1" VALUE=""/> +
<INPUT TYPE="text" NAME="number2" VALUE=""/>
<INPUT TYPE="button" NAME="button" Value="=" onClick="sum()"/>
<INPUT TYPE="text" ID="add" NAME="result" VALUE=""/>
</FORM>
</BODY>
</HTML>
with innerHTML and DIV
<HTML>
<HEAD>
<TITLE>Sum</TITLE>
<script type="text/javascript">
function sum()
{
var num1 = document.myform.number1.value;
var num2 = document.myform.number2.value;
var sum = parseInt(num1) + parseInt(num2);
document.getElementById('add').innerHTML = sum;
}
</script>
</HEAD>
<BODY>
<FORM NAME="myform">
<INPUT TYPE="text" NAME="number1" VALUE=""/> +
<INPUT TYPE="text" NAME="number2" VALUE=""/>
<INPUT TYPE="button" NAME="button" Value="=" onClick="sum()"/>
<DIV ID="add"></DIV>
</FORM>
</BODY>
</HTML>
#1
8
innerHTML
sets the text (including html elements) inside an element. Normally we use it for elements like div, span etc to insert other html elements inside it.
innerHTML在元素中设置文本(包括html元素)。通常,我们将它用于div、span等元素,以便在其中插入其他html元素。
For your case you want to set the value of an input element. So you should use the value
attribute.
对于这种情况,您需要设置输入元素的值。所以你应该使用value属性。
Change innerHTML
to value
改变innerHTML值
document.getElementById('add').value = sum;
#2
3
Do you really want the result to come up in an input box? If not, consider a table with borders set to other than transparent and use
你真的希望结果显示在输入框中吗?如果不是,请考虑设置为透明和使用之外的边框的表
document.getElementById('sum').innerHTML = sum;
. getelementbyid(”和“)。innerHTML =总和;
#3
3
<HTML>
<HEAD>
<TITLE>Sum</TITLE>
<script type="text/javascript">
function sum()
{
var num1 = document.myform.number1.value;
var num2 = document.myform.number2.value;
var sum = parseInt(num1) + parseInt(num2);
document.getElementById('add').value = sum;
}
</script>
</HEAD>
<BODY>
<FORM NAME="myform">
<INPUT TYPE="text" NAME="number1" VALUE=""/> +
<INPUT TYPE="text" NAME="number2" VALUE=""/>
<INPUT TYPE="button" NAME="button" Value="=" onClick="sum()"/>
<INPUT TYPE="text" ID="add" NAME="result" VALUE=""/>
</FORM>
</BODY>
</HTML>
This should work properly. 1. use .value instead of "innerHTML" when setting the 3rd field (input field) 2. Close the input tags
这应该正常工作。1。在设置第3个字段(输入字段)2时,使用.value代替“innerHTML”。关闭输入标签
#4
0
With .value and INPUT tag
<HTML>
<HEAD>
<TITLE>Sum</TITLE>
<script type="text/javascript">
function sum()
{
var num1 = document.myform.number1.value;
var num2 = document.myform.number2.value;
var sum = parseInt(num1) + parseInt(num2);
document.getElementById('add').value = sum;
}
</script>
</HEAD>
<BODY>
<FORM NAME="myform">
<INPUT TYPE="text" NAME="number1" VALUE=""/> +
<INPUT TYPE="text" NAME="number2" VALUE=""/>
<INPUT TYPE="button" NAME="button" Value="=" onClick="sum()"/>
<INPUT TYPE="text" ID="add" NAME="result" VALUE=""/>
</FORM>
</BODY>
</HTML>
with innerHTML and DIV
<HTML>
<HEAD>
<TITLE>Sum</TITLE>
<script type="text/javascript">
function sum()
{
var num1 = document.myform.number1.value;
var num2 = document.myform.number2.value;
var sum = parseInt(num1) + parseInt(num2);
document.getElementById('add').innerHTML = sum;
}
</script>
</HEAD>
<BODY>
<FORM NAME="myform">
<INPUT TYPE="text" NAME="number1" VALUE=""/> +
<INPUT TYPE="text" NAME="number2" VALUE=""/>
<INPUT TYPE="button" NAME="button" Value="=" onClick="sum()"/>
<DIV ID="add"></DIV>
</FORM>
</BODY>
</HTML>