本文介绍了我制作了一个用于添加两个数字的JavaScript代码。但是有一些错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按下添加按钮时,我只想在两个文本框中添加两个数字并得到总和。

但是有一个错误,我无法弄清楚。感谢您是否可以帮助我。

以下是代码:

I just want to add two numbers to the two text boxes and get the sum, when I press the Add button.
But there is an error and I can''t figure it out. Appreciate if you can help me.
Here is the code:

<html>
<head>
<script type="text/javascript">
	function AddTwo(x,y)
	{
		var x;
		var y;
		var sum;
		sum=num1+num2;

		document.write("Summation is "+sum);
		}
</script>
</head>

<body>
	<label>Number 1</label>
	<input type="text" name="num1" value="" />
    <label>Number 2</label>
    <input type="text" name="num2" value="" />
    <br />
	<input type="button" onclick="AddTwo(parseInt(num1.value),parseInt(num2.value))" value="Add Numbers" />
</body>
</html>

推荐答案


<script type="text/javascript">
	function AddTwo(num1,num2)
	{
		var sum;
		sum=num1+num2;
		document.write("Summation is "+sum);
		}
</script></script>


onclick="AddTwo(parseInt(num1.value),parseInt(num2.value))"

所以,我在两个文本框中都添加了id属性。



So, I added id attribute to both the textboxes.

<html>
<head>
<script type="text/javascript">
	function AddTwo(x,y)
	{
		var x;
		var y;
		var sum;

		sum = x + y; - This line is changed

		document.write("Summation is " + sum);
		return false;
		}
</script>
</head>

<body>
	<label>Number 1</label>
	<input type="text" name="num1" value="" id="num1"/>

    <label>Number 2</label>
    <input type="text" name="num2" value="" id="num2"/>

    <br />

	<input type="button" onclick="AddTwo(parseInt(num1.value),parseInt(num2.value))" value="Add Numbers" />
</body>
</html>



现在这段代码工作正常。

谢谢......


Now this code is working fine.
Thanks...


这篇关于我制作了一个用于添加两个数字的JavaScript代码。但是有一些错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:02
查看更多