鍍金池/ 問(wèn)答/HTML/ 遍歷input到數(shù)組對(duì)象

遍歷input到數(shù)組對(duì)象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
    
    <form class="form-horizontal" role="form">
        <div class="form-group">
            <label class="control-label">money:</label>
            <div>
                <input type="text" name="money" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">age:</label>
            <div>
                <input type="text" name="age" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">height:</label>
            <div>
                <input type="text" name="height" class="form-control">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2">
                <button type="button">保存</button>
            </div>
        </div>
    </form>
    
    <script>
        // 1、遍歷所有的 input 取到它們的value 和 name 值存到數(shù)組對(duì)象里;
        // 2、新手有點(diǎn)傷腦筋,求老師父指點(diǎn)一下下。
        var postdata = {
            id:'',
            title:'',
            content:'',
            forms:[]
        }
        var forms = {
            label:'',
            value:''
        }
        // 現(xiàn)在是想把input 的 name 存到 forms.label里 
        // input的value 存到 forms.value 里;
        // 然后再把 forms 添加到 postdata.forms數(shù)組里。
        // 好難阿 >_<  我的是思路是先給所有的 input 一個(gè)class valiform 
        // 然后 jQuery $.each valiform ; 
        // $.each(valiform,function(i,v){
        //     myforms.label = $(this).attr('name');
        //         myforms.value = $(this).val();
        //         saveData.forms[i] = myforms;
        //     });
        //  這樣是錯(cuò)的。搞不定了!
    </script>
</body>
</html>
回答
編輯回答
不歸路

你看下 這個(gè)效果是否是你需要的效果

var postdata = {
            id:'',
            title:'',
            content:'',
            forms:{}
        }
        var forms = {
            label:[],
            value:[]
        }
        document.querySelectorAll('input').forEach(function (e,i){
            forms.label.push(e.name);
            forms.value.push(e.value);
        });
        postdata.forms=forms;
        console.log(forms);
        console.log(postdata.forms);

還有一點(diǎn)就是 postdata.forms 你這邊是個(gè)數(shù)組對(duì)象,而下面的 forms又是個(gè)JSON對(duì)象,雖然對(duì)調(diào)用對(duì)象沒(méi)什么區(qū)別,但是最好還是改成同類型

望早日解決問(wèn)題~

2017年10月22日 14:00
編輯回答
真難過(guò)

其實(shí)你思路基本是對(duì)的

var inputs = document.querySelectorAll('input')
var forms = Array.prototype.map.call(inputs, input => {
    return {
        label: input.getAttribute('name'),
        value: input.value
    }
})

其中 inputs 是所有要提取屬性的 input 的數(shù)組。

2017年11月3日 19:16
編輯回答
憶往昔
$.each(valiform,function(i,v){
postdata.forms.push({
    label: $(this).attr('name'),
    value: $(this).val()
});
});
2018年5月3日 03:18
編輯回答
六扇門
Array.from(document.querySelectorAll('input'),e=>{const obj={};obj[e.name]=e.value;return obj})
2018年2月8日 06:48