Bạn có thể tạo một polyfill thực hiện trên gửi biểu mẫu. Ví dụ:
/* Attach the form event when jQuery loads. */
$(document).ready(function(e){
/* Handle any form's submit event. */
$("form").submit(function(e){
e.preventDefault(); /* Stop the form from submitting immediately. */
var continueInvoke = true; /* Variable used to avoid $(this) scope confusion with .each() function. */
/* Loop through each form element that has the required="" attribute. */
$("form input[required]").each(function(){
/* If the element has no value. */
if($(this).val() == ""){
continueInvoke = false; /* Set the variable to false, to indicate that the form should not be submited. */
}
});
/* Read the variable. Detect any items with no value. */
if(continueInvoke == true){
$(this).submit(); /* Submit the form. */
}
});
});
Kịch bản này chờ đợi đối với hình thức phải nộp, sau đó vòng mặc dù mỗi yếu tố hình thức mà có thuộc tính required
có giá trị nhập vào. Nếu mọi thứ đều có giá trị, nó sẽ gửi biểu mẫu.
Một yếu tố ví dụ để được kiểm tra có thể là:
<input type="file" name="file_input" required="true" />
(Bạn có thể loại bỏ những ý kiến & giảm bớt mã này khi sử dụng nó trên trang web của bạn)
Nguồn
2013-07-15 09:30:08
Bạn sẽ cần JavaScript. –
@MadaraUchiha tùy chọn javascript cũng tốt. Làm ơn nói đi. – Bere
Vui lòng cung cấp thêm tệp HTML. Tôi cho rằng bạn có một nút gửi ở đâu đó? Đính kèm một hàm javascript xác nhận xem trường nhập tệp có trống hay không và nếu trống thì sẽ trả về một thông báo lỗi. – Tro