Traversing input to array objects

<!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 ;
        // 2
        var postdata = {
            id:"",
            title:"",
            content:"",
            forms:[]
        }
        var forms = {
            label:"",
            value:""
        }
        // input  name  forms.label 
        // inputvalue  forms.value ;
        //  forms  postdata.forms
        //  >_<   input class valiform 
        //  jQuery $.each valiform ; 
        // $.each(valiform,function(i,v){
        //     myforms.label = $(this).attr("name");
        //         myforms.value = $(this).val();
        //         saveData.forms[i] = myforms;
        //     });
        //  
    </script>
</body>
</html>
Mar.18,2021

$.each(valiform,function(i,v){
postdata.forms.push({
    label: $(this).attr('name'),
    value: $(this).val()
});
});

in fact, you are basically right


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);

another point is postdata.forms. You have an array object on your side, and the following forms is a JSON object. Although there is no difference to the calling object, it is best to change it to the same type

.

look forward to solving the problem as soon as possible ~

Menu