Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính checked trong HTML


Giới thiệu

Thuộc tính checked trong HTML dùng để chỉ định rằng một phần tử <input> (kiểu checkbox hoặc radio) được chọn sẵn (pre-selected) khi trang web tải. Đây là thuộc tính boolean (chỉ cần xuất hiện là true).

Cú pháp

<input type="checkbox" checked>
<input type="radio" checked>

Ví dụ minh họa (Checkbox)

<!DOCTYPE html>
<html>
<body>
  <h2>Đăng ký</h2>
  <form action="" method="get">
    ID: <input type="text" name="id" placeholder="Nhập UserId..." size="25"><br>
    Mật khẩu: <input type="password" name="pwd" placeholder="Nhập mật khẩu..."><br>
    Ngày sinh: <input type="date" name="dob" placeholder="Nhập ngày sinh..."><br>
    Điện thoại: <input type="tel" name="tel" placeholder="Nhập số điện thoại..."><br>
    Email: <input type="email" name="email" placeholder="Nhập email..." size="35"><br><br>
    <input type="checkbox" name="vehicle" value="Bike" checked> Đăng ký nhận bản tin: <br>
    <button type="submit" value="Submit">Gửi</button>
  </form>
</body>
</html>

Kết quả hiển thị

Ô checkbox "Đăng ký nhận bản tin" được tích sẵn khi trang tải nhờ thuộc tính checked.

<input type="checkbox" name="vehicle" value="Bike" checked> Đăng ký nhận bản tin: