问题:通过从文本框中的表单中获取两个操作数和一个运算符来实现基本计算器应用程序。还有一个名为result的按钮,当单击相同时显示结果。只有页面的结果部分必须更改
我的代码在这里,但它根本不起作用,请帮我显示输出
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script>
function showResult(id,url)
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var val1=document.form1.value1.value;
var valu1=parseInt(val1);
var val2=document.form1.value2.value;
var valu2=parseInt(val2);
var val3=document.form1.operator.value;
var result;
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("id").innerHTML=xmlhttp.responseText;
}
}
if("+"==val3)
{
result=valu1+valu2;
//alert(result);
}
else if("-".equals(val3))
{
result=val1-val2;
}
else if("*".equals(val3))
{
result=val1*val2;
}
else if("/".equals(val3))
{
result=val1/val2;
}
else
{
alert("choose oly +,-,*,/");
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form name=form1 method=get>
<input type=text name="value1">
<input type=text name="value2">
<input type=text name="operator">
<input type=button value="Result" name=result onClick="showResult('ajaxDiv','BasicCalc.html')">
</form>
<div id="ajaxdiv"> Your Result soon will be display here</div>
</body>
</html>