javascript - 重复表复选框仅返回原始值而不是重复项

标签 javascript jquery

嗨,这实际上最初是在另一个问题( HERE )中,该问题有两个部分。第一部分由@epascarello 解决了 - 再次感谢,现在只剩下这部分了,我似乎不明白。

我正在创建一个包含复选框的可重复 div,提交时它仅返回控制台中原始输入的一个值,但不会返回任何重复项的值。

非常感谢任何帮助。

JS fiddle :http://jsfiddle.net/dawidvdh/EEd7c/

jQuery:

//Clone Tracking
var g_counter = 1;
var d_counter = 1;
var dependant = ["dependant"];
var group;
//Clone Tracking
//General Variables
var relation_input_groups = ["relation-group-1"];
//General Variables
//Generate variables
var relation_fields=[0];
var relation_input ="<label>Spouse</label>"+

                    "<input type='checkbox' value='spouse' class='relationship' name='relationship' />" +
                    "<label>own child</label>"+ 

                    "<input type='checkbox' value='ownchild' class='relationship' name='relationship' />" +
                    "<label>adopted</label>"+ 

                    "<input type='checkbox' value='adopted' class='relationship' name='relationship' />" +
                    "<label>stepchild</label>"+ 

                    "<input type='checkbox' value='stepchild' class='relationship' name='relationship' />" +
                    "<label>parent</label>"+ 

                    "<input type='checkbox' value='parent' class='relationship' name='relationship' />" +
                    "<label>inlaw</label>"+ 

                    "<input type='checkbox' value='inlaw' class='relationship' name='relationship' />" +
                    "<label>brother</label>"+ 

                    "<input type='checkbox' value='brother' class='relationship' name='relationship' />" +
                    "<label>other</label>"+ 

                    "<input type='checkbox' value='other' class='relationship' name='relationship' />";
//Generate variables
jQuery(document).ready(function(e) {
    //populate jquery generated fields
    jQuery(relation_fields).each(function() {
        jQuery(relation_input).appendTo('#relation-group-1');
    });
    //populate jquery generated fields
    //Cloning Function
    jQuery('#clone').click(function() {
        clone_dependant();
    });
    function clone_dependant() {
        // Store the value of the previous Id to insert the cloned div..
        var oldId = g_counter;
        g_counter++;
        currentdep ='dependant-'+g_counter;
        // Clone the Dependant Div and set a new id
        var $clonedDiv = jQuery('#dependant-1').clone(false).attr('id', 'dependant-'+g_counter);
        var relation_newDiv = 'relation-group-'+ g_counter;
        // Find div's inside the cloned object and set a new id's
        $clonedDiv.find('#relation-group-1').attr('id',"relation-group-" + g_counter );
        // You don't need to Loop thru the inputs to set the value
        $clonedDiv.find('input').val('');
        $clonedDiv.find('input:checkbox').removeAttr('checked');
        // Insert the cloned object 
        $clonedDiv.insertAfter("#dependant-" + oldId);

        relation_input_groups.push(relation_newDiv);
    }
    //Cloning Function
    //Validation
//submit function
$(document).on("click", 'input[type="checkbox"]', function() {
    jQuery(this).siblings(":checked").removeAttr('checked');
});

var result = {};
var dependants;
var dep_counter = 0;
jQuery('#submit').click(function(){
    jQuery('.dependant').each(function(k, v){
        dep_counter++
        dependants = {};
        result['dependant'+dep_counter] = [dependants];
        dependants['relationship'] = $(v).find('.relationship:checked').val();
    });
    var jsonData = JSON.stringify(result);
    console.log(jsonData);
});
});

和 HTML:

<div id="dependant-1" class="dependant">
    <div id="label">relationship:</div> <div id="relation-group-1"></div>
</div>

<button id="clone">clone</button>
<button id="submit">submit</button>

最佳答案

查看此更新的 fiddle :http://jsfiddle.net/EEd7c/7/

注释此行:$clonedDiv.find('input').val('');

同时在提交按钮单击时设置dep_counter = 0;..

关于javascript - 重复表复选框仅返回原始值而不是重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14360329/

相关文章:

javascript - 像这样使用时 Object.prototype 的语义是什么

javascript - 查找父兄弟 td 隐藏值

javascript - MVC.Grid : how to call @Html. 使用jquery的AjaxGrid

javascript - 面向对象 JS : Is there point using prototype property in the base object?

JavaScript - 函数调用后的垃圾收集

javascript - 不寻常的 JavaScript 语法

javascript - 如何使用 JavaScript 或 jQuery 读取 json 数据

javascript - 同步动画,或在动画中途调用

jquery - jQuery 弹出对话框中的 Chrome html5 表单验证

javascript - 使用 setInterval 的简单 javascript 函数出错