现在的位置: 首页 > 综合 > 正文

判断多个是否有已经选择好文件的

2012年10月15日 ⁄ 综合 ⁄ 共 790字 ⁄ 字号 评论关闭
  表单中有多个<input type="file" name="uploadfile" contentEditable="false" style="width:80%">,
提交表单时需要判断其中至少要有一个input已经选择好文件。
  
1 <input type="file" name="uploadfile" contentEditable="false" style="width:80%"><br>
2 <input type="file" name="uploadfile" contentEditable="false" style="width:80%"><br>
3 <input type="file" name="uploadfile" contentEditable="false" style="width:80%"><br>
4 <input type="file" name="uploadfile" contentEditable="false" style="width:80%"><br>
5 <input type="file" name="uploadfile" contentEditable="false" style="width:80%">

  使用jQuery进行判断:

  

 1 var fileFlag = false;
 2 $("input[name='uploadfile']").each(function(){
 3     if($(this).val()!="") {
 4         fileflag = true;
 5         return false;
 6     }
 7 });
 8 if(fileFlag) {
 9     alert("已有选择好文件的");
10 }

  只要fileFlag为true,就可以退出each循环,不需要再对剩下的input进行判断。

  在each中使用return false退出循环,使用return true结束当前次循环,进行下一次循环。

【上篇】
【下篇】

抱歉!评论已关闭.