2013-08-26 100 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/

相關問題