1 回答

TA貢獻(xiàn)14條經(jīng)驗 獲得超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()" />
??? <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->
??? <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('請輸入數(shù)字!');
??????? } else if (a.length == 0) {
??????????? alert('請輸入數(shù)字!');
??????? } else if (b.length == 0) {
??????????? alert('請輸入數(shù)字!');
??????? } else if (b == 0) {
??????????? alert('除數(shù)不能為0!');
??????? }
??? }
??? </script>
</body>
</html>
count()函數(shù)你最后少了個}括號;
parseInt(document.getElementById('txt1').value)。parseInt的應(yīng)該是到value的后面,你也寫錯位置了
添加回答
舉報