代碼
提交代碼
<input type='text' id = 'num1'> + <input type='text' id='num2'> = <input type='text' id='res'>
<button onclick='count()'>計算結果</button><!-- 點擊按鈕實現計算 -->
<script>
function count(){
if(isNaN(document.getElementById('num1').value) || isNaN(document.getElementById('num2').value)) return alert("請輸入正確的數字");//判斷輸入數字是否合法
document.getElementById('res').value = parseFloat(document.getElementById("num1").value) + parseFloat(document.getElementById("num2").value);//將結果輸出
}
</script>
運行結果