# JQuery 檢查上傳多個檔案附件檔案大小 ```htmlmixed=\ <th></font>附件上傳</th> <td colspan=3> <span style="font-weight: bold;">附加檔案1:</span> <s:file label="匯入" name="uploadFile" /> <br> <span style="font-weight: bold;">附加檔案2:</span> <s:file label="匯入" name="uploadFile" /> <br> <span style="font-weight: bold;">附加檔案3:</span> <s:file label="匯入" name="uploadFile" /> <br> </td> ``` ```javascript=\ $(document).ready(function() { $("input[name=uploadFile]").change(function(){ checkFile(); }); }); function checkFile() { var fileSize= 0; var totalSize = 0 ; jQuery('input[name="uploadFile"]').each(function(i, item) { if(item.files.item(0) != null){ fileSize = item.files.item(0).size; totalSize = totalSize+fileSize ; } }); if(checkFileSize(totalSize)){ return true; }else{ return false; } } //檢查檔案大小 function checkFileSize(totalSize) { var SizeLimit = 1024*1024*10; //上傳上限(10MB),單位:byte if (totalSize > SizeLimit) { var msg = "附件已超過上傳上限 "+(SizeLimit /(1024*1024)) + " MB\n不允許上傳!"; alert(msg); return false; } else { return true; } } ```