Jquery追加 - 附加双?

时间:2013-04-28 16:45:25

标签: jquery append

为什么这段代码会为每个元素附加两次对象?

$('input[data-type="answer"]').each(function (index, input) {
    var name = $(input).attr('name');
    var value = $(input).val();
    $("#QuizQuestion").append('<div id="QuizQuestionHeadline"></div><div class="control-group"><label class="radio"><input type="radio" class="input-fluid" value="' + value + '" />' + value + '</label></div>');
});

谢谢!


意识到我发布了错误的代码,这里是:

Jquery创建答案:

var newAnswerAlt = "appendAnswerAltRadio" + i++;
    $("<div><div class='controls'><input type='text' class='answerAltRadiobtn' data-type='answer' class='input-fluid' name='txtAnswer"+ n++ +"'> <label class='checkbox'><input type='checkbox' id='QuizCheckbox"+ c++ +"'> Correct Answer</label></div></div>").attr({id: newAnswerAlt, class: ''}).prependTo(".NewQuizAnswersGroup").hide().fadeIn('slow');
    });

Jquery从创建的答案中获取属性并将它们附加到另一个元素:

    $('input[data-type="answer"]').each(function(index, input){
        var name = $(input).attr('name');
        var value = $(input).val();     
        var question = "txtQuestion1";      
        $("#QuizQuestion").append('<div id="QuizQuestionHeadline"></div><div class="control-group"><label class="radio"><input type="radio" class="input-fluid" name="' + question + '" value="'+value+'" />'+value+'</label></div>');          
        });

    var NewQuizQuestion = $("#NewQuizQuestion").val();
    $("#QuizQuestionHeadline").append("<legend>"+NewQuizQuestion+"</legend>");  

     $('input[data-type="UserDetails"]').each(function(index, input){
    var name = $(input).attr('name');
    var label = $("label[for='"+name+"']").text()
    var value = $(input).is(":checked");
    if($(input).is(":checked")){
        $("#formAlts").append('<div id="formHeader"></div><div class="control-group"><label>'+label+'</label><div class="controls"><input type="text" class="input-fluid" name="' + name + '" /></div></div>');
    }
    });

    var formHeader = "Fill out form";
    $("#formHeader").append("<legend>"+formHeader+"</legend>");

    var terms = "Accept Terms & Conditions";
    $("#terms").append("<div class='control-group'><label class='checkbox'><input type='checkbox' name='txtTerms'>"+terms+"</label></div>");    

第二次调用“'input [data-type =”UserDetails“]'”工作正常但不适用于''input [data-type =“answer”]'“。

无法理解它!

0 个答案:

没有答案