要求文件输入以包含文件

时间:2015-02-12 11:49:56

标签: javascript jquery

您可以在运行函数之前检查输入文件类型是否包含文件。除非用户输入文件,否则我需要以某种方式禁用提交按钮。

<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>

1 个答案:

答案 0 :(得分:1)

我能想到的一种方法是在输入值变化上绑定一个函数并设置一个标志

var flag = false;
$('#uploaderForm input[type="file"]').bind('change', function(event){
   if(event && event.target && event.target.files[0]){
      flag = true;
   }
});