我对 JavaScript 相当陌生,并且在分配作业时遇到了麻烦。我需要从 2 个文本字段中获取用户输入,然后对它们进行加、减、乘或除,然后显示答案。我也不确定如何显示答案,我一直试图让它出现在段落中,但我似乎无法让它发挥作用。提前致谢!
JavaScript
<script>
var number=new Object();
number.Num1="";
number.Num2="";
function processData(Num1,Num2) {
// get all the fields' values
number.Num1 = document.getElementById(Num1).value;
number.Num2 = document.getElementById(Num2).value;
//Check which radio button is checked
if (document.getElementById('Add').checked)
//Shoot out answer
return Num1+Num2;
//Write answer
document.getElementById("Answer").innerHTML=processData(Num1,Num2)
}
</script>
HTML 表单
<table><tr>
<td height="600" align="center"><form id="form1" name="form1" method="post" action="">
<p>Input the two numbers that you wish to calculate!</p>
<p>
<label>First Number!
<input type="text" name="Num1" id="Num1" />
</label>
</p>
<p>
<label>Second Number!
<input type="text" name="Num2" id="Num2" />
</label>
</p>
<p>Please choose what you wish to do with the numbers!</p>
<p>
<label>
<input type="radio" name="Add" value="Add" id="Add" />
Add</label>
<br />
<label>
<input type="radio" name="Multiply" value="Multiply" id="Multiply" />
Multiply</label>
<br />
<label>
<input type="radio" name="Substract" value="Substract" id="Substract" />
Substract</label>
<br />
<label>
<input type="radio" name="Divide" value="Divide" id="Divide" />
Divide</label>
</p>
<p>Click Submit to get your answer!</p>
<p>
<label>Click me!
<input type="submit" name="Button" id="Button" value="Submit" onclick="processData('Num1','Num2')" />
</label>
</p>
<p>Your answer is : </p> <p id="Answer"> </p><br />
</form></td>
</tr>
</table>