您可以在运行功能之前检查输入文件类型中是否包含文件。我需要以某种方式禁用“提交”按钮,除非用户输入文件。
<script>
$(function() {
var bar = $('.progress-bar');
var percent = $('.percent');
var status = $('#status');
$('#uploaderForm').ajaxForm({
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
$( "#progressBar" ).removeClass('disappear').addClass('appear');
$( "#submitFileUpload" ).addClass('disappear');
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
//console.log(percentVal, position, total);
},
success: function() {
var percentVal = '100%';
bar.width(percentVal)
percent.html(percentVal);
},
complete: function() {
$( "#status" ).text("Great, your upload was completed successfully.").removeClass('disappear');
$( "#progressBar" ).removeClass('appear').addClass('disappear');
setTimeout(function() {
window.location.href = "/thanks";
}, 2000);
},
});
})();
</script>
最佳答案
我能想到的一种方法是在输入值更改时绑定一个函数并设置一个标志
var flag = false;
$('#uploaderForm input[type="file"]').bind('change', function(event){
if(event && event.target && event.target.files[0]){
flag = true;
}
});