index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Counter</title>
</head>
<body>
<div>
<input id="i1" type="text" name="n1"/>
+
<input id="i2" type="text" name="n2"/>
=
<input id="i3" type="text" name="n3"/>
<input type="button" value="原生Ajax" onclick="get_sum()">
</div>
<script>
var i1 = document.getElementById("i1");// 获取三个input
var i2 = document.getElementById("i2");
var i3 = document.getElementById("i3");
function get_sum() {
var n1 = i1.value; // 获取input里的value
var n2 = i2.value;
i3.value = parseInt(n1)+parseInt(n2); //str转int
}
</script>
</body>
</html>
todo:
这里我想用ajax将两个数字发到后台来求和, 突然发现ajax好像只用它来自动访问url获取过数据.没主动传过数据到后台过呢.