Tôi có một hình thức tải lên tập tin đơn giản. Làm cách nào để tôi tự động gửi nó khi tệp đã được chọn? Tôi không muốn người dùng phải nhấp vào nút Gửi.
Tôi có một hình thức tải lên tập tin đơn giản. Làm cách nào để tôi tự động gửi nó khi tệp đã được chọn? Tôi không muốn người dùng phải nhấp vào nút Gửi.
Câu trả lời:
Bạn chỉ có thể gọi submit
phương thức của biểu mẫu trong trường onchange
hợp nhập tệp của bạn.
document.getElementById("file").onchange = function() {
document.getElementById("form").submit();
};
Submit()
lệnh gọi kích hoạt submit
sự kiện jQuery , nhưng không kích hoạt biểu mẫu bên dưới submit()
. Tất nhiên, bạn có thể sử dụng $('form')[0].submit()
để nhấn phần tử DOM bằng jQuery
Chỉ cần nói với file
-input để tự động gửi biểu mẫu về bất kỳ thay đổi nào:
<form action="http://example.com">
<input type="file" onchange="form.submit()" />
</form>
Giải pháp này hoạt động như thế này:
onchange
làm cho phần tử đầu vào thực thi đoạn script sau, bất cứ khi nào value
được sửa đổiform
tham chiếu biểu mẫu, rằng phần tử đầu vào này là một phần củasubmit()
khiến biểu mẫu gửi tất cả dữ liệu tới URL, như được chỉ định trong action
Ưu điểm của giải pháp này:
id
s. Nó làm cho cuộc sống dễ dàng hơn, nếu bạn có một vài hình thức trong một trang html.Sử dụng jQuery:
$('#file').change(function() {
$('#target').submit();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="target" action="destination.html">
<input type="file" id="file" value="Go" />
</form>
JavaScript với onchange
sự kiện:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="filename" onchange="javascript:this.form.submit();">
</form>
$('#fileInput').change(function() {
$('#myForm').submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<form action="upload.php" id="myForm">
<input type="file" id="fileInput">
</form>
Đây là giải pháp tải lên hình ảnh của tôi, khi người dùng chọn tệp.
Phần HTML:
<form enctype="multipart/form-data" id="img_form" method="post">
<input id="img_input" type="file" name="image" accept="image/*">
</form>
JavaScript:
document.getElementById('img_input').onchange = function () {
upload();
};
function upload() {
var upload = document.getElementById('img_input');
var image = upload.files[0];
$.ajax({
url:"/foo/bar/uploadPic",
type: "POST",
data: new FormData($('#img_form')[0]),
contentType:false,
cache: false,
processData:false,
success:function (msg) {}
});
};
Hãy thử mã dưới đây với jquery:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<script>
$(document).ready(function(){
$('#myForm').on('change', "input#MyFile", function (e) {
e.preventDefault();
$("#myForm").submit();
});
});
</script>
<body>
<div id="content">
<form id="myForm" action="action.php" method="POST" enctype="multipart/form-data">
<input type="file" id="MyFile" value="Upload" />
</form>
</div>
</body>
</html>
Đối với những người đang sử dụng .NET WebForms, việc gửi toàn bộ trang có thể không được mong muốn. Thay vào đó, hãy sử dụng cùng một onchange
ý tưởng để có javascript nhấp vào nút ẩn (ví dụ: <asp: Nút ...) và nút ẩn có thể mất phần còn lại. Hãy chắc chắn rằng bạn đang làm một display: none;
nút trên và không Visible="false"
.
HTML
<form id="xtarget" action="upload.php">
<input type="file" id="xfilename">
</form>
JAVASCRIPT PURE
<script type="text/javascript">
window.onload = function() {
document.getElementById("xfilename").onchange = function() {
document.getElementById("xtarget").submit();
}
};
</script>
<form action="http://example.com">
<input type="file" onchange="Submit()" />
</form>
<script>
// it will submit form 0 or you have to select particular form
document.getElementsByTagName("form")[0].submit();
</script>
Bạn có thể đặt mã này để làm cho mã của bạn hoạt động chỉ với một dòng mã
<input type="file" onchange="javascript:this.form.submit()">
Điều này sẽ tải tệp lên máy chủ mà không cần nhấp vào nút gửi