如何使用值查找所有输入

问题描述 投票:0回答:1

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

$(":input").not("[id*=a_prefix_]").map(function() 
    { 
        var value = this.val(); 
        if( value ) console.log(this.id + ":=" + value ); 
    }
);
  1. this.val()存在问题,我收到错误“对象不支持属性或方法'val'”,解决方案是什么?
  2. 如何移动检查以查看实际选择中是否存在值,以便地图仅获取值的输入?
javascript jquery jquery-selectors
1个回答
1
投票
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();
© www.soinside.com 2019 - 2024. All rights reserved.