JS căn bản] -id368 - 3
RUN
<html> <body> <script language="javascript"> // Hàm tính kết quả function tinh() { // Lấy 3 ô input var a = document.getElementById("a"); var b = document.getElementById("b"); var result = document.getElementById("result"); // Tính tổng hai ô đầu tiên var tong = parseInt(a.value) + parseInt(b.value); // Gán giá trị vào ô thứ ba // Phải kiểm tra tổng hai số này có bị lỗi hay không if (!isNaN(tong)){ result.value = tong; } } </script> a: <input type="text" id="a" value="" onkeyup="tinh()"/> b: <input type="text" id="b" value="" onkeyup="tinh()"/> Kết quả: <input type="text" id="result" value=""/> </body> </html>
PHÓNG TO