1 回答
TA贡献14条经验 获得超10个赞
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
</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' />
<script type="text/javascript">
function count() {
var a = parseInt(document.getElementById('txt1').value);
var b = parseInt(document.getElementById('txt2').value);
var c = document.getElementById('select').value;
var d;
if (c == '+') {
d = a + b;
document.getElementById('fruit').value = d;
} else if (c == '-') {
d = a - b;
document.getElementById('fruit').value = d;
} else if (c == '*') {
d = a * b;
document.getElementById('fruit').value = d;
} else if (c == '/') {
d = a / b;
document.getElementById('fruit').value = d;
} else if (isNaN(a) || isNaN(b)) {
alert('请输入数字!');
} else if (a.length == 0) {
alert('请输入数字!');
} else if (b.length == 0) {
alert('请输入数字!');
} else if (b == 0) {
alert('除数不能为0!');
}
}
</script>
</body>
</html>
count()函数你最后少了个}括号;
parseInt(document.getElementById('txt1').value)。parseInt的应该是到value的后面,你也写错位置了
添加回答
举报