哪位大神幫我看看,感覺搞不出效果
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?var a,b,c,d;
? ?a=parseInt(document.getElementById(txt1).value); ? ?
? ? //獲取第一個輸入框的值
? ?b=parseInt(document.getElementById(txt2).value);
//獲取第二個輸入框的值
? ?c=document.getElementById(select).value;
? ? //獲取選擇框的值
? ? switch (c)
? ? {case +:d=a+b break;
? ? ?case -:d=a-b break;
? ? ?case *:d=a*b break;
? ? ?default d=a/b}
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? document.getElementById("fruit").value = parseInt(d);
? ? //設置結果輸入框的值?
? ??
? ?}
? </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>
2016-07-08
有5個錯誤 ?1?a=parseInt(document.getElementById(txt1).value); 應寫成? a=document.getElementById("txt1").value; ?b句也改成這樣的樣式 ?txt1 txt2 需加雙引號 ??2 ? + - * / ?得加雙引號 ?3 ? d=a+b 這四句后面得加 分號 ? 4? d=a+b ?這四個表達式寫錯了 ?你輸入的a和b都是字符串 ? 需要parseInt()進行解析,再由它返還一個整數 ? 所以 ? d=parseInt(a)+parseInt(b) ? ?5 ??document.getElementById("fruit").value = parseInt(d); ? 這句應該寫成?document.getElementById("fruit").value = d;
2016-07-05
這個是其它同學的代碼
2016-07-05
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?var d = "";
? ? var a=document.getElementById("txt1").value; ??
? ? //獲取第一個輸入框的值
? ? var b=document.getElementById("txt2").value;
//獲取第二個輸入框的值
? ? var c=document.getElementById("select").value;
//獲取選擇框的值
? ? switch(c)
{
? ? ?case "+":
? ? ?//d = a + b;
? ? ?d = parseInt(a)+parseInt(b);
? ? ?break;
? ? ?case "-":
? ? ?d = a-b;
? ? ?break;
? ? ?case "*":
? ? ?d = a*b;
? ? ?break;
? ? ?default:
? ? ?d = a/b;
? ? ?}
? ? document.getElementById("fruit").value ? = d;
? ??
? ?}
? </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>
2016-07-05
case +:d=a+b break;1.+用“”包含。2.分成兩個語句3.加法要用parseInt函數,別的可以不用
2016-07-05
+-*/要用引號括起來