我在jquery中做了一个循环,以便可以获取所有我拖过的图像的列表。我可以获取图像列表,但现在尝试将其推入数组中,以便可以在代码中的其他位置使用它。
这是我的js代码
$(document).ready(function(){
var dropZone = document.getElementById('drop-zone');
var filename_list = [];
dropZone.ondrop = function(e){
e.preventDefault();
for(x = 0; x < e.dataTransfer.files.length; x++)
{
//Gets the list of names from when I dragged images over
var filename = e.dataTransfer.files[x].name
//Pushing the list of filenames into an array
filename_list.push(filename);
}
}
//Trying to use the filename array outside of the loop
console.log('filename - '+filename_list);
dropZone.ondragover = function(e){
return false;
}
dropZone.ondragleave = function(){
return false;
}
});
此处的代码段
$(document).ready(function() {
var dropZone = document.getElementById('drop-zone');
var filename_list = [];
dropZone.ondrop = function(e) {
e.preventDefault();
for (x = 0; x < e.dataTransfer.files.length; x++) {
//Gets the list of names from when I dragged images over
var filename = e.dataTransfer.files[x].name
//Pushing the list of filenames into an array
filename_list.push(filename);
}
}
//Trying to use the filename array outside of the function
console.log('filename - ' + filename_list);
dropZone.ondragover = function(e) {
return false;
}
dropZone.ondragleave = function() {
return false;
}
});
h1,
h2,
h3,
h4,
h5 {
font-weight: 400;
}
.hidden {
display: none;
}
.wrapper {
width: 100%;
max-width: 600px;
margin: 0 auto;
}
.upload-console {
background: #fefefe;
border: 2px solid #eee;
padding: 20px;
}
.upload-console-header {
padding: 0 0 20px 0;
margin: 0;
border-bottom: 2px solid #eee;
font-weight: 600;
margin-bottom: 20px;
}
.upload-console-drop {
height: 200px;
border: 2px dashed #ccc;
line-height: 200px;
color: #ccc;
text-align: center;
margin-bottom: 20px;
}
.upload-console-drop.drop {
border-color: #222;
color: #222;
}
.upload-console-body {
margin-bottom: 20px;
}
.bar {
width: 100%;
background: #eee;
padding: 3px;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
box-sizing: border-box;
margin-bottom: 20px;
}
.bar-fill {
height: 30px;
display: block;
background: cornflowerblue;
width: 0;
border-radius: 3px;
-webkit-transition: width 0.8s ease;
transition: width 0.8s ease;
}
.bar-fill-text {
color: #fff;
line-height: 30px;
margin-left: 5px;
}
.upload-console-upload {
border-bottom: 2px solid #ccc;
margin-bottom: 10px;
padding-bottom: 10px;
}
.upload-console-upload span {
float: right;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label for="content">content</label>
<textarea name="content" id="content" class="form-control" cols="30" rows="10"></textarea>
</div>
<input type="file" name="files[]" id="standard-upload-files" multiple>
<input type="submit" value="Upload files" id="standard-upload">
<div class="wrapper">
<div class="upload-console">
<h2 class="upload-console-header">
Upload
</h2>
<div class="upload-console-body">
<div class="upload-console-drop" id="drop-zone">
just drag and drop files here
</div>
<div class="bar">
<div class="bar-fill" id="bar-fill">
<div class="bar-fill-text" id="bar-fill-text"></div>
</div>
</div>
<div class="hidden" id="uploads-finished">
<h3>Process files</h3>
<div class="upload-console-upload">
<a href="#">filename.jpg</a>
<span>Success</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-dark btn-lg btn-block" name="add_product" id="add_item">Save</button>
</div>
</form>
这是我的代码的一部分。 CODEPEN
答案 0 :(得分:1)
您需要在填充全局(不好的情况下这样做)或在我同时显示两者的事件之后查看数组。
如果您选择一个,我也会添加该更改事件。
$(function() {
// I could also put both events here if I wanted
$("#standard-upload-files").on('change', function(event) {
let files = $(this).val();
console.log('Change files:', files);
});
var dropZone = document.getElementById('drop-zone');
var filename_list = [];
dropZone.ondrop = function(e) {
e.preventDefault();
for (x = 0; x < e.dataTransfer.files.length; x++) {
//Gets the list of names from when I dragged images over
var filename = e.dataTransfer.files[x].name
//Pushing the list of filenames into an array
filename_list.push(filename);
// trigger AND pass, no global needed
$(this).trigger('got-dropped', [filename_list]);
}
}
//fails as this happens before it is populated
console.log('filename:', filename_list);
$('#drop-zone').on('got-dropped', function(event, files) {
console.log('filename:', filename_list);
console.log("files:", files);
});
$('input[type=file]').val()
dropZone.ondragover = function(e) {
return false;
}
dropZone.ondragleave = function() {
return false;
}
});
h1,
h2,
h3,
h4,
h5 {
font-weight: 400;
}
.hidden {
display: none;
}
.wrapper {
width: 100%;
max-width: 600px;
margin: 0 auto;
}
.upload-console {
background: #fefefe;
border: 2px solid #eee;
padding: 20px;
}
.upload-console-header {
padding: 0 0 20px 0;
margin: 0;
border-bottom: 2px solid #eee;
font-weight: 600;
margin-bottom: 20px;
}
.upload-console-drop {
height: 200px;
border: 2px dashed #ccc;
line-height: 200px;
color: #ccc;
text-align: center;
margin-bottom: 20px;
}
.upload-console-drop.drop {
border-color: #222;
color: #222;
}
.upload-console-body {
margin-bottom: 20px;
}
.bar {
width: 100%;
background: #eee;
padding: 3px;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
box-sizing: border-box;
margin-bottom: 20px;
}
.bar-fill {
height: 30px;
display: block;
background: cornflowerblue;
width: 0;
border-radius: 3px;
-webkit-transition: width 0.8s ease;
transition: width 0.8s ease;
}
.bar-fill-text {
color: #fff;
line-height: 30px;
margin-left: 5px;
}
.upload-console-upload {
border-bottom: 2px solid #ccc;
margin-bottom: 10px;
padding-bottom: 10px;
}
.upload-console-upload span {
float: right;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label for="content">content</label>
<textarea name="content" id="content" class="form-control" cols="30" rows="10"></textarea>
</div>
<input type="file" name="files[]" id="standard-upload-files" multiple>
<input type="submit" value="Upload files" id="standard-upload">
<div class="wrapper">
<div class="upload-console">
<h2 class="upload-console-header">
Upload
</h2>
<div class="upload-console-body">
<div class="upload-console-drop" id="drop-zone">
just drag and drop files here
</div>
<div class="bar">
<div class="bar-fill" id="bar-fill">
<div class="bar-fill-text" id="bar-fill-text"></div>
</div>
</div>
<div class="hidden" id="uploads-finished">
<h3>Process files</h3>
<div class="upload-console-upload">
<a href="#">filename.jpg</a>
<span>Success</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-dark btn-lg btn-block" name="add_product" id="add_item">Save</button>
</div>
</form>
答案 1 :(得分:0)
请注意,console.log('filename - '+filename_list)
将始终在设置项目(文件名)之前运行。这是因为只有在调用dropZone.ondrop事件时才会填充filename_list。如果您真的想在其他地方使用更新的filename_list,则有两种方法。
下面是代码的更新版本。已经证明了这两种方法。
$(document).ready(function(){
var dropZone = document.getElementById('drop-zone');
var filename_list = [];
dropZone.ondrop = function(e){
e.preventDefault();
for(x = 0; x < e.dataTransfer.files.length; x++)
{
//Gets the list of names from when I dragged images over
var filename = e.dataTransfer.files[x].name
//Pushing the list of filenames into an array
filename_list.push(filename);
console.log('sss',filename_list);
}
//1. You pass it to a function
yourCallback(filename_list);
//2. Or you can triger another event and listen to it elsewhere
$(this).trigger('filename_updated',[filename_list]) ;
}
//Trying to use the filename array outside of the function
//A simple callback function
function yourCallback(list){
console.log('Your filename_list - ',list);
}
$('#drop-zone').on('filename_updated', function(event, filenameList) {
console.log("Your filename_list from filename_updated event", filenameList);
console.log("Your filename_list from global filename_list variable", filename_list);
});
dropZone.ondragover = function(e){
return false;
}
dropZone.ondragleave = function(){
return false;
}
});
h1,
h2,
h3,
h4,
h5{
font-weight: 400;
}
.hidden{
display: none;
}
.wrapper{
width: 100%;
max-width: 600px;
margin: 0 auto;
}
.upload-console{
background: #fefefe;
border: 2px solid #eee;
padding: 20px;
}
.upload-console-header{
padding: 0 0 20px 0;
margin: 0;
border-bottom: 2px solid #eee;
font-weight: 600;
margin-bottom: 20px;
}
.upload-console-drop{
height: 200px;
border: 2px dashed #ccc;
line-height: 200px;
color: #ccc;
text-align: center;
margin-bottom: 20px;
}
.upload-console-drop.drop{
border-color: #222;
color: #222;
}
.upload-console-body{
margin-bottom: 20px;
}
.bar{
width: 100%;
background: #eee;
padding: 3px;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, .2);
box-sizing: border-box;
margin-bottom: 20px;
}
.bar-fill{
height: 30px;
display: block;
background: cornflowerblue;
width: 0;
border-radius: 3px;
-webkit-transition: width 0.8s ease;
transition: width 0.8s ease;
}
.bar-fill-text{
color: #fff;
line-height: 30px;
margin-left: 5px;
}
.upload-console-upload{
border-bottom: 2px solid #ccc;
margin-bottom: 10px;
padding-bottom: 10px;
}
.upload-console-upload span{
float: right;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label for="content">content</label>
<textarea name="content" id="content" class="form-control" cols="30" rows="10"></textarea>
</div>
<input type="file" name="files[]" id="standard-upload-files" multiple>
<input type="submit" value="Upload files" id="standard-upload">
<div class="wrapper">
<div class="upload-console">
<h2 class="upload-console-header">
Upload
</h2>
<div class="upload-console-body">
<div class="upload-console-drop" id="drop-zone">
just drag and drop files here
</div>
<div class="bar">
<div class="bar-fill" id="bar-fill">
<div class="bar-fill-text" id="bar-fill-text"></div>
</div>
</div>
<div class="hidden" id="uploads-finished">
<h3>Process files</h3>
<div class="upload-console-upload">
<a href="#">filename.jpg</a>
<span>Success</span>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-dark btn-lg btn-block" name="add_product" id="add_item">Save</button>
</div>
</form>
致谢