如何在jquery中将所有输入字段值获取到数组?

min*_*004 -1 javascript jquery

如何在jquery中将所有输入字段值获取到数组?

请看下面的代码:

<input type="text" name="a" value="a" />
<input type="text" name="b" value="hello" />
<input type="text" name="c" value="world" />

<script type="text/javascript">
    function get_fields_values(){

        // too much code
        var arr=[];
        $.each($("input"),function(i,n){
            arr.push(n.value)
        }); 
        return arr;

        // is there any jquery method to get fields values to an array?
        // $('input').xxx() ?
    }
</script>
Run Code Online (Sandbox Code Playgroud)

Raj*_*amy 5

尝试使用.map().get()来完成你的任务,

var arr = $("input").map(function(){ return this.value; }).get();
Run Code Online (Sandbox Code Playgroud)

完整的代码是,

function get_fields_values(){
    return $("input").map(function(){ return this.value; }).get();
}
Run Code Online (Sandbox Code Playgroud)

演示