<form method="get" action="calc.php" id="myForm">
num1:
<input id="num1" type=text name="num1" value="" />
<br/> num2:
<input id="num2" type=text name="num2" value="" />
<br/> oper:
<select name="oper" value="">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<br/>
<input type="submit" value="提交" />
<script>
var subBtn = document.getElementById("myForm");
var num1Val = document.getElementById("num1").value;
var num2Val = document.getElementById("num2").value;
subBtn.onsubmit = function () {
alert(num1Val);
return false;
};
</script>
</form>
- 要用表单提交,不要用下面的input提交;
- 页面已经加载完毕,num1Val 已经获取到值,value的值为空,所以提交的数据为空,需要点击提交时获取值:
var subBtn = document.getElementById("myForm");
subBtn.onsubmit = function () {
var num1Val = document.getElementById("num1").value;
var num2Val = document.getElementById("num2").value;
alert(num1Val);
return false;
};