如何在多文件上传器中启用提交按钮直到条件?
How to enable submit button until condition in a multiple file uploader?
我正在做一个网络应用程序,人们会上传 2 张图像,我需要通过 python 代码。因此,我只想让他们在上传2张图片时发送。
我一直在阅读其他帖子,例如:
How to disable submit button until file is selected
Enable submit button after uploading more than 1 file
但我无法将其推断到我的案例中:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.css">
</head>
<body>
<!-- Uploader -->
<div class="custom-file-container" data-upload-id="myImage">
<label>Upload your images <a href="javascript:void(0)" class="custom-file-container__image-clear" title="Clear Image">×</a></label>
<label class="custom-file-container__custom-file" >
<form id="upload-form" action="{{url_for('upload')}}" method="POST" enctype="multipart/form-data">
<input type="file" class="custom-file-container__custom-file__custom-file-input" accept="image/*" aria-label="Choose File" multiple>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
<span class="custom-file-container__custom-file__custom-file-control"></span>
</label>
<div class="custom-file-container__image-preview"></div>
</div>
<input type="submit" name="send" disabled/>
</div>
<script src="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.js"></script>
<script>
var upload = new FileUploadWithPreview('myImage', {showDeleteButtonOnImages: true, text: {chooseFile: 'Nom del fitxer', browse: 'Examina'}})
</script>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(
function(){
$('input:file').change(
function(){
if ($(this).val()) {
$('input:submit').attr('disabled',false);;
}
}
);
});
</script>
</body>
</html>
最后添加的脚本是第一个link中给出的解决方案,可以正常工作,但不是我想要的。
文件上传器代码来自 https://github.com/promosis/file-upload-with-preview,其中我看到有一个名为 selectedFilesCount 的方法可能很有用。
感谢您抽出宝贵时间,如果您在我的代码中看到一些废话,我深表歉意,但我是这些语言的新手...
您可以挂接到上传对象的 upload.cachedFileArray
以检查 array
长度以验证是否已选择 2 个文件进行上传。这是通过 window event listeners
fileUploadWithPreview:imageSelected
和 fileUploadWithPreview:imageDelete
绑定的 toggle() 函数检查的,因此如果图像在被选中后被删除,您仍然可以执行规则 2:
$(document).ready(function() {
});
var toggle = function() {
//console.log(upload.cachedFileArray.length);
if (upload.cachedFileArray.length == 2) {
$('input:submit').attr('disabled', false);
} else {
$('input:submit').attr('disabled', true);
}
};
window.addEventListener('fileUploadWithPreview:imageSelected', function(e) {
// optionally you could pass the length into the toggle function as a param
// console.log(e.detail.cachedFileArray.length);
toggle();
});
window.addEventListener('fileUploadWithPreview:imageDeleted', function(e) {
// optionally you could pass the length into the toggle function as a param
// console.log(e.detail.cachedFileArray.length);
toggle();
});
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.css">
</head>
<body>
<!-- Uploader -->
<div class="custom-file-container" data-upload-id="myImage">
<label>Upload your images <a href="javascript:void(0)" class="custom-file-container__image-clear" title="Clear Image">×</a></label>
<label class="custom-file-container__custom-file">
<form id="upload-form" action="{{url_for('upload')}}" method="POST" enctype="multipart/form-data">
<input type="file" class="custom-file-container__custom-file__custom-file-input" accept="image/*" aria-label="Choose File" multiple>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
<span class="custom-file-container__custom-file__custom-file-control"></span>
</label>
<div class="custom-file-container__image-preview"></div>
</div>
<input type="submit" name="send" disabled/>
</div>
<script src="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.js"></script>
<script>
var upload = new FileUploadWithPreview('myImage', {
showDeleteButtonOnImages: true,
text: {
chooseFile: 'Nom del fitxer',
browse: 'Examina'
}
});
</script>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
</body>
</html>
我正在做一个网络应用程序,人们会上传 2 张图像,我需要通过 python 代码。因此,我只想让他们在上传2张图片时发送。
我一直在阅读其他帖子,例如:
How to disable submit button until file is selected
Enable submit button after uploading more than 1 file
但我无法将其推断到我的案例中:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.css">
</head>
<body>
<!-- Uploader -->
<div class="custom-file-container" data-upload-id="myImage">
<label>Upload your images <a href="javascript:void(0)" class="custom-file-container__image-clear" title="Clear Image">×</a></label>
<label class="custom-file-container__custom-file" >
<form id="upload-form" action="{{url_for('upload')}}" method="POST" enctype="multipart/form-data">
<input type="file" class="custom-file-container__custom-file__custom-file-input" accept="image/*" aria-label="Choose File" multiple>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
<span class="custom-file-container__custom-file__custom-file-control"></span>
</label>
<div class="custom-file-container__image-preview"></div>
</div>
<input type="submit" name="send" disabled/>
</div>
<script src="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.js"></script>
<script>
var upload = new FileUploadWithPreview('myImage', {showDeleteButtonOnImages: true, text: {chooseFile: 'Nom del fitxer', browse: 'Examina'}})
</script>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).ready(
function(){
$('input:file').change(
function(){
if ($(this).val()) {
$('input:submit').attr('disabled',false);;
}
}
);
});
</script>
</body>
</html>
最后添加的脚本是第一个link中给出的解决方案,可以正常工作,但不是我想要的。
文件上传器代码来自 https://github.com/promosis/file-upload-with-preview,其中我看到有一个名为 selectedFilesCount 的方法可能很有用。
感谢您抽出宝贵时间,如果您在我的代码中看到一些废话,我深表歉意,但我是这些语言的新手...
您可以挂接到上传对象的 upload.cachedFileArray
以检查 array
长度以验证是否已选择 2 个文件进行上传。这是通过 window event listeners
fileUploadWithPreview:imageSelected
和 fileUploadWithPreview:imageDelete
绑定的 toggle() 函数检查的,因此如果图像在被选中后被删除,您仍然可以执行规则 2:
$(document).ready(function() {
});
var toggle = function() {
//console.log(upload.cachedFileArray.length);
if (upload.cachedFileArray.length == 2) {
$('input:submit').attr('disabled', false);
} else {
$('input:submit').attr('disabled', true);
}
};
window.addEventListener('fileUploadWithPreview:imageSelected', function(e) {
// optionally you could pass the length into the toggle function as a param
// console.log(e.detail.cachedFileArray.length);
toggle();
});
window.addEventListener('fileUploadWithPreview:imageDeleted', function(e) {
// optionally you could pass the length into the toggle function as a param
// console.log(e.detail.cachedFileArray.length);
toggle();
});
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.css">
</head>
<body>
<!-- Uploader -->
<div class="custom-file-container" data-upload-id="myImage">
<label>Upload your images <a href="javascript:void(0)" class="custom-file-container__image-clear" title="Clear Image">×</a></label>
<label class="custom-file-container__custom-file">
<form id="upload-form" action="{{url_for('upload')}}" method="POST" enctype="multipart/form-data">
<input type="file" class="custom-file-container__custom-file__custom-file-input" accept="image/*" aria-label="Choose File" multiple>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
<span class="custom-file-container__custom-file__custom-file-control"></span>
</label>
<div class="custom-file-container__image-preview"></div>
</div>
<input type="submit" name="send" disabled/>
</div>
<script src="https://unpkg.com/file-upload-with-preview@3.4.3/dist/file-upload-with-preview.min.js"></script>
<script>
var upload = new FileUploadWithPreview('myImage', {
showDeleteButtonOnImages: true,
text: {
chooseFile: 'Nom del fitxer',
browse: 'Examina'
}
});
</script>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
</body>
</html>