标签: javascript jquery
我有一个具有属性class='check_quantity'
class='check_quantity'
$('.check_quantity').each(function(){ quantities.push($(this).val()); });
当我使用console.log(数量)时;
["1", ""]
它输出如此,为什么数组中有一个空值?
答案 0 :(得分:0)
在jquery中使用 map() 将值转换为array()
var quantities=$('.check_quantity').map(function(){ if ($.trim(this.value) != "") { // this.value.trim() != "" it is pure java script better to use this return this.value; } }).get();
注意:.check_quantity有一些空值(没有值),所以在这种情况下你必须检查空值并返回
.check_quantity
<强> DEMO 强>