Javascript 獲取用戶輸入,條件判斷

<body> 
<html>    
<script>
     function f() {
            try {
                //獲取用戶輸入的數據
                var x = document.getElementById("demo").value;
                if (x == "") throw "值爲空";
                if(isNaN(x)) throw "不是數字";
              if(x>=10 )     throw "太大";
              if(x<=5)      throw "太小";
            }
            //e 接受throw 的錯誤信息
            catch (e) {
                var y=document.getElementById("ee");
                y.innerHTML = "錯誤" + e
            }
        }
</script>
<p>請輸入一個數字</p>
<input id = "demo" type="text">
<button type = button  onclick="f()">測試輸入</button>
<p id="ee"></p>

</body>
</html>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章