如果从文本框里面接收参数 再通过onclick调用add3()该怎么写?
<body> <form id="root"> <input type="text" id="num1" placeholder="第一个数"> <input type="text" id="num2" placeholder="第二个数"> <input type="text" id="num3" placeholder="第三个数"> <input type="submit" value="求和"> </form> <p id="result"></p> </body> <script type="text/javascript"> document.getElementById("root").addEventListener("submit",add3); //绑定事件,调用方法 function add3(e){ e.preventDefault(); //阻止默认事件 var num1 = parseInt(document.getElementById("num1").value); //获取表单值,转为数值类型 var num2 = parseInt(document.getElementById("num2").value); var num3 = parseInt(document.getElementById("num3").value); var sum = 0; sum = num1 + num2 +num3; document.getElementById("result").innerHTML = "结果为:"+sum; } </script>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>返回值函数</title>
<script type="text/javascript">
function app2(x,y)
{ var sum,x,y;
sum = x * y;
return sum;
}
function getvalue()
{
// alert("1111");
//获取文本框中输入的值
var nu1 = document.getElementById("num1").value;
var nu2 = document.getElementById("num2").value;
req1 = app2(nu1,nu2);
document.write(req1);
}
</script>
</head>
<body>
<input type="text" id='num1' /><br/>
<input type="text" id='num2'/><br/>
<input type="button" value="点击提交" onclick="getvalue()">
</body>
</html>