Thương hiệu mới với JS. Tôi đang cố gắng kiểm tra xem phần tử đầu vào tệp có trống không khi gửi biểu mẫu với JQuery/JavaScript. Tôi đã trải qua một loạt các giải pháp và không có gì phù hợp với tôi. Tôi đang cố gắng tránh /c/fakepath
[trừ khi không có lựa chọn nào khác]
I am trying to check if the file input element is empty when submitting the form with jQuery/JavaScript. I have gone through a bunch of solutions and nothing is working for me. I am trying to avoid the /c/fakepath
[unless there is no other option]
Điều này không hoạt động:
var vidFile = $["#videoUploadFile"].value;
Cách duy nhất tôi có thể nhận được tên tệp là nếu tôi sử dụng như sau:
var vidFile = document.getElementById["videoUploadFile"].files[0].name;
Nếu không có tệp có sẵn, mã ném lỗi:
Không thể đọc tên tài sản của không xác định
mà có ý nghĩa vì mảng không được đặt. Nhưng tôi không thể tìm ra cách xử lý lỗi với điều này.
Làm cách nào để lấy đúng phần tử đầu vào tệp videoUploadFile
, kiểm tra xem nó có trống không, ném thông báo lỗi nếu trống?
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận Use element.files.length property to check file is selected or not. If element.files.length property returns 0 then the file is not selected otherwise file is selected.
Cho một tài liệu HTML chứa phần tử đầu vào và tác vụ là kiểm tra xem phần tử đầu vào có trống hay không với sự trợ giúp của JavaScript. This example implements the above approach.
var vidFile = $["#videoUploadFile"].value;
2
var vidFile = $["#videoUploadFile"].value;
6var vidFile = $["#videoUploadFile"].value;
7var vidFile = $["#videoUploadFile"].value;
6var vidFile = $["#videoUploadFile"].value;
9Cách tiếp cận 1: Sử dụng phần tử.files.length để kiểm tra tệp có được chọn hay không. Nếu thuộc tính phần tử.files.length trả về 0 thì tệp không được chọn nếu không, tệp được chọn.
var vidFile = document.getElementById["videoUploadFile"].files[0].name;
1var vidFile = $["#videoUploadFile"].value;
0var vidFile = document.getElementById["videoUploadFile"].files[0].name;
3Ví dụ: Ví dụ này thực hiện phương pháp trên.
var vidFile = $["#videoUploadFile"].value;
2var vidFile = document.getElementById["videoUploadFile"].files[0].name;
1var vidFile = $["#videoUploadFile"].value;
4var vidFile = document.getElementById["videoUploadFile"].files[0].name;
3var vidFile = $["#videoUploadFile"].value;
6videoUploadFile
1var vidFile = $["#videoUploadFile"].value;
2var vidFile = document.getElementById["videoUploadFile"].files[0].name;
1/c/fakepath
5var vidFile = document.getElementById["videoUploadFile"].files[0].name;
3
var vidFile = $["#videoUploadFile"].value;
6
5var vidFile = document.getElementById["videoUploadFile"].files[0].name;
3var vidFile = $["#videoUploadFile"].value;
2var vidFile = document.getElementById["videoUploadFile"].files[0].name;
1videoUploadFile
8var vidFile = document.getElementById["videoUploadFile"].files[0].name;
3var vidFile = $["#videoUploadFile"].value;
2
var vidFile = $["#videoUploadFile"].value;
2
3 /c/fakepath
0>
55____23 Use element.files.length property in jQuery to check the file is selected or not. If element.files.length property returns 0 then the file is not selected otherwise file is selected.Cho một tài liệu HTML chứa phần tử đầu vào và tác vụ là kiểm tra xem phần tử đầu vào có trống hay không với sự trợ giúp của JavaScript. This example implements the above approach.
var vidFile = $["#videoUploadFile"].value;
46