0

divに続いてクローンを作成していますが、すべてのクローンにnewidを作成したいと思います。そしてそれはそのようにやっていますが、それはdivを別々に作成し、入力を別々のoutsidedivとすべてのデザインmessupとして作成しています。次のdiv_cloneを使用して、すべての入力を他のdivコンテナに表示する必要があります。

HTML

<div id="div_" style="display: none;" class="well form-inline">
    <label>Label :</label>    <input type="text"  id="txtlabel" name="txtlabel" />
    <label>value :</label>    <input type="text"  id="txtvalue" name="txtvalue" />
    <label>Selected :</label> <input type="checkbox" name="chk_sel" value="chk_sel" />
    <label>Required :</label> <input type="checkbox" name="chk_isreq" value="chk_isreq" />
</div>

jQuery

var = question_cnt=1;
$("#div_").clone().find("input,label").andSelf().each(function() {
    $(this).show();
    $(this).attr('id', this.id +question_cnt );
    $(this).attr('name', this.name +question_cnt );
}).appendTo("#container").end();
question_cnt++;
4

1 に答える 1

1

これはおそらくあなたがやろうとしていることだと思いますが、私もlabels をループすることに戸惑っていますか? idそれらにはももnameもありませんfor。これは理にかなっています。なぜそれらをループするのですか?

$(document).ready(function(){
    var $underscore = $("#div_"),
        question_cnt = 0;

    function clone() {
        var $clone = $underscore.clone();

        $clone
            .find("input, label")
            .each(function() {
                $(this)
                    .attr({
                        id: this.id + question_cnt,
                        name: this.name + question_cnt
                    });       
            });

        $clone
            .appendTo("#container")
            .show();

        question_cnt++;
    }

    $('#cloneit').click(clone);
});

http://jsfiddle.net/userdude/M2xjp/

于 2012-06-28T07:24:15.710 に答える