js做一个简单的加减乘除计算,程序求改正
<inputid="txt1"type="text"/><selectid="operator"><optionvalue="+">+</option><optionva...
<input id="txt1" type="text" />
<select id="operator"> <option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input id="txt2" type="text" />
<input id="btn" type="button" value="=" onclick="return btn_onclick()" />
<input id="txt3" type="text" />
<script type="text/javascript">
function validate(str) {
var reg = /^\d+$/;
if (!reg.test(str)) {
alert("请输入数字");
}
}
function btn_onclick() {
var txt1 = document.getElementById("txt1").value;
var opra = document.getElementById("operator").value;
var txt2 = document.getElementById("txt2").value;
var btn = document.getElementById("btn").value;
var txt3 =0;
switch (opra) {
case "+":
txt3 = parseInt(txt1) + parseInt(txt2);
break;
case "-":
txt3 = parseInt(txt1) - parseInt(txt2);
break;
case "*":
txt4 = parseInt(txt1) * parseInt(txt2);
break;
case "/":
if (parseInt(txt2) != 0) {
txt3 = parseInt(txt1) / parseInt(txt2);
}
else {
alert("除数不能为0");
}
return;
default:
break;
}
document.getElementById(" txt3").value = txt3;
} 展开
<select id="operator"> <option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input id="txt2" type="text" />
<input id="btn" type="button" value="=" onclick="return btn_onclick()" />
<input id="txt3" type="text" />
<script type="text/javascript">
function validate(str) {
var reg = /^\d+$/;
if (!reg.test(str)) {
alert("请输入数字");
}
}
function btn_onclick() {
var txt1 = document.getElementById("txt1").value;
var opra = document.getElementById("operator").value;
var txt2 = document.getElementById("txt2").value;
var btn = document.getElementById("btn").value;
var txt3 =0;
switch (opra) {
case "+":
txt3 = parseInt(txt1) + parseInt(txt2);
break;
case "-":
txt3 = parseInt(txt1) - parseInt(txt2);
break;
case "*":
txt4 = parseInt(txt1) * parseInt(txt2);
break;
case "/":
if (parseInt(txt2) != 0) {
txt3 = parseInt(txt1) / parseInt(txt2);
}
else {
alert("除数不能为0");
}
return;
default:
break;
}
document.getElementById(" txt3").value = txt3;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |