<input type='file' multiple />) 中最多顯示多少項?)?


若要在 HTML 表單中允許多個檔案上傳,請使用多個屬性。多個屬性適用於電子郵件和檔案輸入型別。


若要限制多個輸入中的最大項數,請使用 JavaScript。透過這個方法,可以限制要上傳的檔案數。例如,假設一次只能上傳 2 個檔案。

你可以嘗試執行以下程式碼來學習如何在 HTML 中使用多個屬性。使用它,我們將會使用 JavaScript 來限制上傳的檔案數量。

示例

即時演示

<!DOCTYPE html>
<html>
   <head>
      <title>HTML file upload</title>
   </head>
   <body>
      <form>
         <input type="file"  action="/action_page.php"  name="name" multiple><br/>
         Upload multiple files, and click Submit.<br>
         <input type = "submit" value = "submit">
      </form>
      <script>
         $(function(){
            $("input[type = 'submit']").click(function(){
               var $fileUpload = $("input[type='file']");
               if (parseInt($fileUpload.get(0).files.length) > 3){
                  alert("You are only allowed to upload a maximum of 3 files");
               }
            });
         });
      </script>
   </body>
</html>

更新於: 2020 年 2 月 25 日

11K+ 閱讀數

開啟您的事業

完成課程以獲得認證

開始使用
廣告
© . All rights reserved.