为什么选择框值(),它必须为值

时间:2014-11-28 05:48:34

标签: javascript jquery

我有一个具有属性class='check_quantity'

的选择框
$('.check_quantity').each(function(){
        quantities.push($(this).val());
    });

当我使用console.log(数量)时;

["1", ""] 

它输出如此,为什么数组中有一个空值?

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有一些空值(没有值),所以在这种情况下你必须检查空值并返回

<强> DEMO

相关问题