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

Thuộc tính type của Input FileUpload trong HTML DOM

Thuộc tính type của <input type="file"> trả về chuỗi "file". Thuộc tính này **chỉ đọc** - không thể thay đổi sau khi tạo phần tử.

Cú pháp

fileInputElement.type

Ví dụ: Lấy loại input file

<!DOCTYPE html>
<html>
<head>
    <style>
        body { text-align: center; background: #52B2CF; color: white; font-family: sans-serif; padding: 20px; }
        .btn {
            background-color: coral; border: none; color: white;
            padding: 10px 20px; border-radius: 25px; width: 60%;
            margin: 20px auto; display: block; cursor: pointer;
        }
        .show { font-size: 1.5rem; font-weight: bold; margin-top: 15px; }
    </style>
</head>
<body>
    <h1>Ví dụ DOM FileUpload type</h1>
    <input type="file" class="file-upload-btn">
    <button onclick="getType()" class="btn">Lấy giá trị type</button>
    <div id="result" class="show"></div>
    <script>
        function getType() {
            var fileBtn = document.querySelector(".file-upload-btn");
            document.getElementById("result").textContent = "type = " + fileBtn.type;
        }
    </script>
</body>
</html>

Kết quả

Trước khi nhấn:

Thuộc tính type của Input FileUpload trong HTML DOM

Sau khi nhấn nút "Lấy giá trị type":

Thuộc tính type của Input FileUpload trong HTML DOM

ℹ️ Lưu ý: Luôn trả về "file" cho <input type="file">. Dùng để kiểm tra loại input trong code xử lý form động.