4 回答
TA贡献1条经验 获得超1个赞
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type='text' name="txt1" id='txt1'/>
<select id='select'>
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type='text' name="txt2" id='txt2'/>
<input type='button' name="but" value='=' onclick="s()"/>
<input type='text' name="te" id='fruit' value=""/>
</body>
</html>
<script type="text/javascript">
function s(){
var sum = document.getElementById("fruit");
var x = document.getElementById("txt1").value;
var y = document.getElementById("txt2").value;
var he =x*1+y*1;
var cha =x*1-y*1;//减
var ji =x*y*1;//乘
var chu = x/y*1;//除
var select=document.getElementById("select").value;
switch(select){
case "+": sum.value=he; break;
case "-": sum.value=cha;break;
case "*": sum.value=ji; break;
case "/": sum.value=chu;break;
default : sum.value="default";
}
}
</script>
修改 了 下 复制 看看
TA贡献1条经验 获得超0个赞
<!DOCTYPE html> <html> <head> <title> 事件</title> <script type="text/javascript"> function count(){ var sum=0; var x=parseInt(document.getElementById("txt1").value); var y=parseInt(document.getElementById("txt2").value); var select=document.getElementById("select").value; switch(select){ case "+": sum=x+y;break; case "-": sum=x-y;break; case "*": sum=x*y;break; case "/": sum=x/y;break; default : sum="default"; break; } document.getElementById("fruit").value = sum; } </script> </head> <body> <input type='text' id='txt1' /> <select id='select'> <option value='+'>+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type='text' id='txt2' /> <input type='button' value=' = ' onClick="count()"/> <!--通过 = 按钮来调用创建的函数,得到结果--> <input type='text' id='fruit' /> </body> </html>
TA贡献1123条经验 获得超629个赞
switch(select){//select呢?document.getElementById("select")去哪儿了?
ducument.getElementById("fruit").value = sum;这句怎么到了函数外面?document也拼错了
添加回答
举报