2016-11-19 6 views
1

jQueryを通じてHTMLを生成するアンケートを作成しています。以下は私がこれまで行ってきたことのコードです。jQueryを使用して動的に作成されたフィールドは一度に削除されます.1つずつ削除する必要があります。

私は.add-questionクラスに質問を追加しています。私はクリックごとにカウンター値を割り当てました。それはtwo typesテキスト多肢選択です。タイプをの複数選択に変更すると、add choiceのリンクが必要です。 add choiceをクリックすると、新しい入力フィールドがdelete choice linkと一緒に生成されます。

質問を1つ追加するとうまくいきます。しかし、一度に2つの質問を生成したら、タイプをmultiple choiceに変更し、さらにfieldsを追加すると、jQueryは両方の質問にinput fieldsを追加し始めます。フィールドを削除すると、そのIDに関連付けられたすべての入力も削除されます。

テキストタイプ

text type

変更されたタイプの複数の選択肢

changed type to multiple choice

に、それはまた、質問に

Remove choice, On remove choice click it has removed questions 2's input also

を2の入力を削除していると削除]をクリックし選択に、選択肢を削除します0
$(document).ready(function() { 
    $('.btn-success').hide(); 
    var counter = 0; 
    var choice = 1; 
    $('.btn-add').on('click', function(){ 
     counter++; 
     $('.add-question').append('<br>' + '<div class="question_'+counter+'">'+ 
       '<div class="form-group">'+ 
      '<label class="control-label col-md-4" for="question-type">Question Type:</label>'+ 
      '<div class="col-md-4">'+ 
       '<select class="form-control question-type" id="'+counter+'">'+ 
        '<option value="text">Text</option>'+ 
        '<option value="multiple" id="'+counter+'">Multiple Choice</option>'+ 
       '</select>'+ 
      '</div>'+ 
      '</div>'+ 
     '<div class="form-group">'+ 
      '<label class="control-label col-md-4" for="question">Enter Question:</label>'+ 
      '<div class="col-md-4">'+ 
       '<input type="text" name="question[]" class="form-control" />'+ 
      '</div>'+ 
      '<div class="col-md-4">'+ 
       '<button type="button" class="btn btn-danger btn-delete " id="'+counter+'">Delete Question</button>'+ 
      '</div>'+ 
     '</div>'+ 
     '<div class="form-group txt-answer'+counter+'">'+ 
      '<label class="control-label col-md-4" for="answer">Answer:</label>'+ 
      '<div class="col-md-4">'+ 
       '<input type="text" name="answer[]" id="answer" class="form-control" />'+ 
      '</div>'+ 
     '</div>'+ 
     '<hr>'+ 
     '<div>'+ 
     '</div>' 
     ); 
    $('.btn-success').show(); 
    }); 

    $(document).on('click', '.btn-delete' , function() { 
     var buttonId = $(this).attr("id"); 
     $('.question_'+buttonId).remove(); 
     if($('.question_'+buttonId) === "undefined") { 
     $('.btn-success').hide(); 
    } 
    }); 


    $(document).on('change', '.question-type' , function(){ 
//  var value = $('#question-type').val(); 
     var value = $(this).attr("id"); 

     if($(this).val() === "multiple") { 
      $('.txt-answer'+value).replaceWith('<div class="form-group remove-choice'+choice+'">'+  
      '<label class="control-label col-md-4" for="choice">Choice '+choice+':</label>'+ 
      '<div class="col-md-4">'+ 
       '<input type="text" name="choice[]" id="choice" class="form-control" />'+ 
      '</div>'+ 
      '<div class="col-md-2 add-choice">'+ 
      '<label class="" for="choice"></label>'+ 
       '<input type="checkbox" name="flag[]" id="flag" /> Correct?'+ 
       '<button type="button" class="btn btn-link btn-remove" id="'+choice+'">Delete Choice</button>'+ 
      '</div>'+ 
      '<button type ="button" class="btn btn-link col-md-offset-4 btn-choice">Add Choice</button>'+ 
     '</div>' 
     ); 
     } else if($(this).val() === "text") { 
      var choiceId = $(this).attr("id"); 
      $('.remove-choice'+choiceId).replaceWith('<div class="form-group">'+ 
      '<label class="control-label col-md-4" for="answer">Answer:</label>'+ 
      '<div class="col-md-4">'+ 
       '<input type="text" name="answer[]" id="answer" class="form-control" />'+ 
      '</div>'+ 
     '</div>'); 
     } 
    }); 

    $(document).on('click', '.btn-choice', function() { 
    choice++; 
     $('<div class="form-group remove-choice'+choice+'">'+  
      '<label class="control-label col-md-4" for="choice">Choice '+choice+':</label>'+ 
      '<div class="col-md-4">'+ 
       '<input type="text" name="choice[]" id="choice" class="form-control" />'+ 
      '</div>'+ 
      '<div class="col-md-2">'+ 
      '<label class="" for="choice"></label>'+ 
       '<input type="checkbox" name="flag[]" id="flag'+choice+'" /> Correct?'+ 
       '<button type="button" class="btn btn-link btn-remove" id="'+choice+'">Delete Choice</button>'+ 
      '</div>'+ 
      '<br>'+ 
     '</div>').insertBefore('.btn-choice'); 
    }); 

    $(document).on('click', '.btn-remove', function() { 
     var buttonId = $(this).attr("id"); 
     $('.remove-choice'+buttonId).remove(); 
    }); 
}); 

編集:私はそれをチェックして、私はそれよりよく作ることができる方法を教えてくださいいじるための私のコードを追加してい

JSfiddle

+0

[OK]を、これは問題の実例ずに通過するために非常に大きなsnippletです。私がちょっと面白いと思うことの1つは、質問タイプ変更ロジックです。ロジックで置換する際にグローバル 'choice'変数を使用しています。だからもし私がこの権利を読んでいるのであれば、あなたが10の質問エリアを持っていて、それらのいずれかのタイプを変更すると、変更した質問タイプにかかわらず、最後の選択値を使用します。 – Taplar

+0

これは、私が関連するクラスに 'choice number 'を割り当てている実際の問題です。それを基にして、私は物事を操作しています。どのように改善することができます。 –

+0

必要なコンテキスト値をdata- *フィールドとして要素に配置することで試すことができます。 data-choice = "#"などです。要素のイベントでは、 '$(element).data( 'choice')でコンテキスト値を得ることができます。' – Taplar

答えて

1

.insertBefore(これを)変更します。 .insertBefore( '.btn-choice')の代わりに。複数の選択肢のすべての質問に選択肢を追加することを解決します。しかし、まだ問題があります。それは対抗問題であり、複数選択問題のすべてが同じです。これを解決して、すべての「btn-choice」属性に「data-choiceCount」という属性を追加することができます。次に、btn-choiceボタンをクリックしたときに選択カウントに使用できます。

Here fiddle is

変更された行

$(document).on('click', '.btn-choice', function() { 
    var choice = parseInt($(this).attr('data-choiceCount')) + 1; 
    $(this).attr('data-choiceCount', choice); 
    $('<div class="form-group remove-choice'+choice+'">'+  
     '<label class="control-label col-md-4" for="choice">Choice '+choice+':</label>'+ 
     '<div class="col-md-4">'+ 
      '<input type="text" name="choice[]" id="choice" class="form-control" />'+ 
     '</div>'+ 
     '<div class="col-md-2">'+ 
     '<label class="" for="choice"></label>'+ 
      '<input type="checkbox" name="flag[]" id="flag'+choice+'" /> Correct?'+ 
      '<button type="button" class="btn btn-link btn-remove" id="'+choice+'">Delete Choice</button>'+ 
     '</div>'+ 
     '<br>'+ 
    '</div>').insertBefore(this); 
    }); 
+0

この 'data-choiceCount'をどのように追加して使用できますか? –

+0

jtでbtn-choiceを作成するattrを追加します。あなたはフィドルで見ることができます。 if($(this).val()=== "multiple"){スコープ。 ' –

+0

これは正常に動作していますが、これは「

関連する問題