例如有兩個圖片上傳的 input,都必須上傳圖片:
html
選擇文件 1 :<input type="file" name="myfile[]" class="myfile"> 選擇文件 2 :<input type="file" name="myfile[]" class="myfile">
js
if($(".myfile").length == 2){ $(".myfile").each(function(){ if($(this).val() == "") { alert("請上傳圖片!"); flag = 0; } }); }else if($(".myfile").length !=2 ){ alert("圖片不能少於2張!"); }