實在找不出來哪里錯了
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title>?
? <script type="text/javascript">
?function count(){
?????? var result="";
??? var num1=document.getElementById("txt1").value;//獲取第一個輸入框的值
?var num2=document.getElementById("txt2").value;//獲取第二個輸入框的值
?var aa=document.getElementById("select").value;//獲取選擇框的值
?switch(aa)
?{
???? case "+":
???? result=parseInt(num1)+parseInt(num2);
???? break;
???? case"-":
???? result=parseInt(num1)-parseInt(num2);
???? break;
???? case"*":
???? result=parseInt(num1)*parseInt(num2);
???? break;
???? case"/":
???? result=parseInt(num1)/parseInt(num2);
???? break;
???? default;
?}
??? document.getElementById("fruit").value=result;
?? }
? </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-08-31
你把script寫到了頭部,是獲取不了下邊的值的,加一個加載事件。用window.onload=function(){
function count(){
?????? var result="";
??? var num1=document.getElementById("txt1").value;//獲取第一個輸入框的值
?var num2=document.getElementById("txt2").value;//獲取第二個輸入框的值
?var aa=document.getElementById("select").value;//獲取選擇框的值
?switch(aa)
?{
???? case "+":
???? result=parseInt(num1)+parseInt(num2);
???? break;
???? case"-":
???? result=parseInt(num1)-parseInt(num2);
???? break;
???? case"*":
???? result=parseInt(num1)*parseInt(num2);
???? break;
???? case"/":
???? result=parseInt(num1)/parseInt(num2);
???? break;
???? default;
?}
??? document.getElementById("fruit").value=result;
?? }}
2019-08-30
default;這里出現錯誤,建議直接刪掉。
2019-08-29
沒有問題,你重新運行試試
2019-08-28
去掉default
2019-08-27
var result="";這個錯了吧,把等于號后邊的去掉就行了