<html> |
|
| <head> |
| <title> new document </title> ? |
| <script type="text/javascript"> |
| function count(){ |
| var txt1 ? = parseInt( document.getElementById('txt1').value);//獲取第一個(gè)輸入框的值 |
| var txt2 ? = parseInt( document.getElementById('txt2').value);//獲取第二個(gè)輸入框的值 |
| var select = document.getElementById('select').value;//獲取選擇框的值 |
| var result = ''; |
| switch (select) |
| { |
| case '+': |
| result = txt1 + txt2; |
| break; |
| case '-': |
| result = txt1 - txt2; |
| break; |
| case '*': |
| result = txt1 * txt2; |
| break; |
| case '/': |
| result = txt1 / txt2; |
| break; |
| } |
| document.getElementById('fruit').value = result;//設(shè)置結(jié)果輸入框的值 |
| } |
| </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> |
2019-02-14
可以直接var result;不用一定要在聲明變量的同時(shí)賦值。
2019-02-12
這一步是聲明一個(gè)沒(méi)賦值的變量,方便后面用它。任何變量都是需要先聲明然后再賦值的。
2019-02-08
var result是定義變量保存數(shù)據(jù),缺少了就是相當(dāng)于沒(méi)有盒子來(lái)存放東西。