【发布时间】:2019-06-19 02:44:10
【问题描述】:
当我的页面打开时,默认情况下会选中所有复选框,并且在提交表单时,我无法在数组中获取选中复选框的值。用户可能不会单击复选框,因为默认情况下它已全部选中。所以我尝试使用以下方式接收检查值:
var valores = (function() {
var valor = [];
$('input.className[type=checkbox]').each(function() {
if (this.checked)
valor.push($(this).val());
});
return valor;
})();
console.log(valores);
我的复选框代码是:
<div class="form-group" id="documents">
<label> <input id="check_id3" type="checkbox" value="3" class="chk3" checked=""> <span>OTHERS</span>
<br>
</label>
<div style="padding-bottom:5px"></div>
<label> <input id="check_id1" type="checkbox" value="1" class="chk1" checked=""> <span>Invoice</span>
<br>
</label>
<div style="padding-bottom:5px"></div>
<label> <input id="check_id2" type="checkbox" value="2" class="chk2" checked=""> <span>Packing List</span>
<br>
</label>
<div style="padding-bottom:5px"></div>
</div>
【问题讨论】:
-
您可以使用
map()来构建您的检查值数组。有关详细信息,请参阅副本
标签: javascript jquery html