Pri*_*rix 9 validation jquery field count
使用jQuery,我如何计算我的数组字段[]中有多少字段不为空?
输入字段的示例
<input type="file" name="arquivo[]" id="arquivo">
Run Code Online (Sandbox Code Playgroud)
我试图使用地图制作一些东西并获得但是它并不顺利:
var total = 0;
var count = $('#arquivo[value!=""]').map(function() { total = total+1; }).get();
Run Code Online (Sandbox Code Playgroud)
但无论我填充多少个字段,它总是以1的值结束,如果我没有填充0的字段.
Ble*_*der 18
你可以找到lengthjQuery对象,它返回匹配元素的数量:
$('#arquivo[value!=""]').length
Run Code Online (Sandbox Code Playgroud)
但是你确实知道这总会回来0或者1?该id属性仅对一个元素是唯一的,因此您不能多次重复使用它.
例如:
<div id="foo"></div>
<div id="foo"></div>
Run Code Online (Sandbox Code Playgroud)
当你运行:
$('#foo').length;
Run Code Online (Sandbox Code Playgroud)
它返回1,因为只有一个元素应该与存在id的foo.
现在试试这个:
<div class="foo"></div>
<div class="foo"></div>
Run Code Online (Sandbox Code Playgroud)
当你运行:
$('.foo').length;
Run Code Online (Sandbox Code Playgroud)
它回来了2.为什么?因为class可以多次重复使用.
你想做什么?你可以发布一个包含多个输入字段的场景吗?