HTML頁面代碼:html
<html> <head> <meta charset="UTF-8"> <title>HelloWorld</title> <script src="hello.js"></script> </head> <body> <form name="myForm1" action="" method="get"> <p><label>匯款金額:</label> <input type="text" name="txtRemittance"></p> <p><label>匯款手續:</label> <input type="text" name="txtFee"></p> <p><input type="button" value="確 定" name="fir" onclick="getFee()"> </p> </form> <hr> <!--這個地方由於直接拷貝上面代碼,致使沒有設置name屬性,發生過錯誤--> <form name="myForm2" action="" method="get"> <p><label>成績:</label><input type="text" name="txtScore"></p> <p><input type="button" value="確 定" name="fir" onclick="getVerdict()"> </p> </form> <hr> <form name="myForm3" action="" method="get"> <p><label>成績:</label><input type="text" name="txtScore"></p> <p><input type="button" value="確 定" name="fir" onclick="getVerdict1()"> </p> </form> <hr> <form name="myForm4" action="" method="get"> <p><input type="text" name="result"></p> <p><input type="button" value="計 算" onclick="calculator()"></p> <hr> <!-- parseInt:將字符串轉換成一個Int prompt:調用輸入對話框的方法,屬於window對象 --> <p><input type="button" value="計算" onclick="calcF(prompt('請輸入一個數值:'))"></p> </form> <hr> <form name="myForm4" action="" method="get"> </form> </body> </html>
Js代碼:函數
function getFee(){ /* 這裏面有個數據類型的問題,我如今沒有搞清楚,若是是C#,從文本框裏 獲得的全部東西,都會是String類型,這個地方爲何獲得了value後,就 能夠直接進行數學運算。 */ var Remittance = document.myForm.txtRemittance.value; var Fee = Remittance*0.01; if (Fee < 2) { Fee = 2; } document.myForm1.txtFee.value=Fee; } function getVerdict(){ var Score = document.myForm2.txtScore.value; if(Score<60){ alert("不及格"); }else if(Score<79){ alert("中等"); }else if(Score <89){ alert("良好"); }else{ alert("優秀"); } } function getVerdict1(){ var Score = parseInt(document.myForm3.txtScore.value/10); switch (Score) { case 10: case 9: alert("very good.");break; case 8: alert("good");break; case 7: alert("中等");break; case 6: alert("及格");break; default: alert("不及格");break; } } function calculator(){ var i=1;sum=0; while (i <= 100) { sum+=i; i++; } document.myForm4.result.value=sum; } /* 函數的定義: 1.不指定函數名 a.把函數直接賦值給變量 var myFun(參數1,參數2,。。。) b.網頁中事件直接調用函數 window.onload = function(參數1,參數2,。。。) 2.指定函數名 在函數調用中,實參列表中參數的數量、類型和順序能夠與形參列表不匹配 若是形參個數大於實參個數,那麼多出來的形參值爲undefined,反之,多出 來的實參將被忽略。 函數調用: 1.直接調用 2.在表達式中調用 3.在事件中調用 4.其餘函數調用 */ function calcF(x){ var result; result=4*x*x+3*x+2; alert("計算結果:"+result); } /* 系統函數: decodeURI(URI) :解碼指定URI decodeURIComponent():解碼指定URI組件 encodeURI(URI) :把字符串編碼爲URI encodeURIComponent():把字符串編碼爲URI組件 Escape(字符串) :對字符串進行編碼 Eval(字符串) :計算js字符串,並把它當作腳本代碼來執行 isFinite(數字) :判斷是不是無窮大數字 isNaN(參數) :判斷是否不是數字 Boolean(參數) :將參數轉換爲布爾值 Number(參數) :將參數轉換爲數值 String(參數) :將參數轉換爲字符串 Object(參數) :將參數轉換成對象 */