Javascript 如何查找具有值的所有输入

Javascript 如何查找具有值的所有输入,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,目标是列出页面上所有输入的所有值,但不列出没有值的输入,有两个问题: $(":input").not("[id*=a_prefix_]").map(function() { var value = this.val(); if( value ) console.log(this.id + ":=" + value ); } ); 当this.val()出现问题时,我得到一个错误“对象不支持属性或方法‘val’”,解决方案是什么 如何移动检

目标是列出页面上所有输入的所有值,但不列出没有值的输入,有两个问题:

$(":input").not("[id*=a_prefix_]").map(function() 
    { 
        var value = this.val(); 
        if( value ) console.log(this.id + ":=" + value ); 
    }
);
  • 当this.val()出现问题时,我得到一个错误“对象不支持属性或方法‘val’”,解决方案是什么
  • 如何移动检查以查看实际选择中是否有值,从而使映射只获取带值的输入

  • this.value
    $(this.val()
    )。使用
    each()
    而不是
    map()
    ,因为它看起来并不是在试图翻译任何东西。所以我理解过滤器,我唯一的问题是关于map/get(),它看起来像是在返回一个值数组,但我需要返回如下内容:id:=value,我是否简单地更改映射以返回this.id+“:=”+this.value?@aphotographer更新了答案以返回id作为键的对象
    var valueArray = $(":input")
        // filter these things out, whatever they are
        .not("[id*=a_prefix_]")
        // filter only the elements that have a value
        .filter(function(){ return this.value.trim(); })
        // map the values
        .map(function(){ return {[this.id]: this.value}; })
        // turn the results into a real array, not a jQuery object of the values
        .get();