javascript - 统计空文件上传控件

标签 javascript jquery html dom

在动态生成的文件上传控件集中:

<input type="file" name="archivos[]">
<input type="file" name="archivos[]">
<input type="file" name="archivos[]">
// ...

...我可以轻松计算非空的:

// Works fine
var nonEmptyCount = $("input[type='file'][name='archivos[]'][value!='']").length;

但是,当我尝试计算(我实际需要的)时,选择器永远不会匹配任何内容:

// Always zero
var emptyCount = $("input[type='file'][name='archivos[]'][value='']").length;

我不敢相信我需要这个繁琐的代码:

// Works but ugly
var emptyCount = $("input[type='file'][name='archivos[]']").length -
    $("input[type='file'][name='archivos[]'][value!='']").length;

我错过了什么?

最佳答案

一种解决方案是检查迭代器内的值是否为空,例如:

$("#findEmpty").on("click", function() {
  var count = 0;
  $(":file[name='archivos[]']").each(function() {
    if (this.value == "") {
      count++;
    }
  });
  alert(count);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type="file" name="archivos[]" />
<input type='button' value='Check Empty' id='findEmpty' />

关于javascript - 统计空文件上传控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27312862/

相关文章:

javascript - 如何从gmaps RECTANGLE(覆盖)中获取4个顶点坐标?

javascript - 什么时候使用 JavaScript 模板引擎?

javascript - Onclick 不适用于 Knockout 数组绑定(bind)字段

html - Bootstrap 页脚不粘在页面底部

javascript - 使用 HTML+JS 在 Canvas 中绘制 3D

javascript - 如何呈现 jQuery Mobile UI 对象后页面加载

javascript - 无法在渲染方法react js中进行条件渲染

php - PHP 生成表上的 jQuery 表搜索过滤器

javascript - 使用 Jquery 命名 JSON 数组

javascript - 在每个页面上打印一个 HTML 标题