Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从动态HTML数组创建对象_Javascript_Jquery_Html_Arrays - Fatal编程技术网

Javascript 从动态HTML数组创建对象

Javascript 从动态HTML数组创建对象,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,我想以正确的顺序从动态生成的数组中获取一个javascript对象,这样以后我就可以对该对象进行JSONE编码并将其保存到数据库中。(也许有另一种更简单的方法) 这是表格 <form name="second_form" id="second_form" action="#" method="POST"> <a href="#" id="AddChampion" onclick="return false;">Add Champion&l

我想以正确的顺序从动态生成的数组中获取一个javascript对象,这样以后我就可以对该对象进行JSONE编码并将其保存到数据库中。(也许有另一种更简单的方法)

这是表格

 <form name="second_form" id="second_form" action="#" method="POST">            
    <a href="#" id="AddChampion" onclick="return false;">Add Champion</a>
        <div id="ChampionInput">
        </div>
        <input type="submit" name="submit">
    </form>
下面是添加一些字段后的图片


下面是正在工作的jSfiddle:

试试这段代码,我希望我正确理解了您的问题

$("#second_form").submit(function(event) {
    //don't do the default action on submit
    event.preventDefault();     
    //Your object as array for the champions
    var object = [];        
    //Loop through each .Champion-div
    $('.Champion').each(function() {

        //Create a new object for this champion with the value from the input field and an array for the descriptions
        var champion = {
            'name': $(this).find(".ChampionInput").val(),
            'description': []
        };

        //Loop through each description textfields of this champion
        $(this).find('.GeneralChangeDescription').each(function() {
                //add the description to the description array of the champion
                champion.description.push($(this).val());
        });         
        //finally put the champion in your champion array
        object.push(champion);
    });

    //Thats just for you, an alert of the json-object
    alert(JSON.stringify(object));
});

这正是我想做的!我以后能用php w JSONECODE把这个表放到我的数据库中吗?如果你想使用AJAX,你必须考虑将这个对象传输到服务器的方法,然后用
$.AJAX({type:“POST”,url:/urlToYourServerPHPPage),数据:{“data”:object},数据类型:'application/json})发送你的数据…到php将解析接收到的json的服务器
$( "#second_form" ).submit( function(event) {
    $( "#ChampionInput.ChampionInput :input" ).each( function( index, value ) {
        var _value = value;
        var _index = index;
        alert(value);
        $(this).children( ".GeneralChangeDescription").each( function( index, value ) {

        });
    });
});
$("#second_form").submit(function(event) {
    //don't do the default action on submit
    event.preventDefault();     
    //Your object as array for the champions
    var object = [];        
    //Loop through each .Champion-div
    $('.Champion').each(function() {

        //Create a new object for this champion with the value from the input field and an array for the descriptions
        var champion = {
            'name': $(this).find(".ChampionInput").val(),
            'description': []
        };

        //Loop through each description textfields of this champion
        $(this).find('.GeneralChangeDescription').each(function() {
                //add the description to the description array of the champion
                champion.description.push($(this).val());
        });         
        //finally put the champion in your champion array
        object.push(champion);
    });

    //Thats just for you, an alert of the json-object
    alert(JSON.stringify(object));
});