遍历input到数组对象

<!DOCTYPE html>

<html>

<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里 

        // input的value 存到 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>


胡子哥哥
浏览 547回答 4
4回答

拉莫斯之舞

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

潇湘沐

Array.from(document.querySelectorAll('input'),e=>{const&nbsp;obj={};obj[e.name]=e.value;returnobj})

慕妹3146593

你看下 这个效果是否是你需要的效果var postdata = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id:'',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title:'',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content:'',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; forms:{}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var forms = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; label:[],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value:[]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; document.querySelectorAll('input').forEach(function (e,i){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; forms.label.push(e.name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; forms.value.push(e.value);&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; postdata.forms=forms;&nbsp; &nbsp; &nbsp; &nbsp; console.log(forms);&nbsp; &nbsp; &nbsp; &nbsp; console.log(postdata.forms);还有一点就是 postdata.forms 你这边是个数组对象,而下面的 forms又是个JSON对象,虽然对调用对象没什么区别,但是最好还是改成同类型望早日解决问题~
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript