2013-08-26 101 views
1

我通过网络搜索,但没有发现我的问题很多事情。希望这里有人能帮助! 正如我在标题中写的,我想上传多个文件,一次只能输入一个。 我试图使用JQuery来做到这一点,如下所示,但显然它不起作用! 任何人都可以帮忙吗?JQuery/PHP多个文件一次只有一个输入字段

<!doctype html> 
    <html> 
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <head> 
    <script> 
    $(document).ready(function() { 
     $(document).on('change', '.file',function(){ 
      $('<input type="file" name="file[]" class="file" size="60" />').appendTo($("#divAjout")); 
      $("div input:first-child").remove();  
      return false; 
     }); 
    }); 
    </script> 

    <title>test input file unique pour plusieurs fichiers</title> 
    </head> 

    <body> 
    <form action="" method="post" enctype='multipart/form-data'> 
    <div id="divAjout"> 
     <input type="file" name="file[]" class="file" id='file' size="60" /> 
     </div> 


     <input name="submit" type="submit"> 
    </form> 
    <?php  if(isset($_POST['submit'])){echo'<pre>'; print_r($_FILES);echo'<pre>';} ?> 
    </body> 
    </html> 
+1

首先在谷歌搜索给了我:http://davidwalsh.name/multiple-file-upload –

+0

为什么你删除第一个输入,当你添加一个新的?该文件尚未上传。 – Barmar

+0

继承人另一个与JQuery完成http://blueimp.github.io/jQuery-File-Upload/ – cmorrissey

回答

0

可以克隆输入文件与递增名称属性,像文件[0],文件[1],文件[2],等等

function readURL(input) { 
       var index = ($(input).data('index')); 
       if (input.files && input.files[0]) { 
        var reader = new FileReader(); 
        reader.onload = function (e) { 
         $(input).parent().children('.newImage') 
          .attr('src', e.target.result) 
          .height(120); 
        }; 
        reader.readAsDataURL(input.files[0]); 

        $('.addPh').show(); 
        $('.addPh').last().clone().insertBefore($('#addPhoto')).hide(); 
        ++index; 
        $('.addPh').last().children('.photo_new').data('index', index).attr('name', 'photo_new['+index+']'); 
       } 
} 

$(document).ready(function() { 
    $('#addPhoto').click(function() { 
     $('.photo_new').last().click(); 
    }); 

}); 

见下面的例子:https://jsfiddle.net/w0cd3Ls4/3/

相关问题