附加的html总是添加到最后动态创建的div中

时间:2018-10-03 13:00:41

标签: javascript jquery

我试图将输入字段和按钮附加到具有ID的动态创建的div中。每次单击另一个按钮,var i都会增加一个,该数字会添加到我要附加HTM1的ID中。

// Category
var i = 1;
$('#addcategory').click(function() {
  i++;
  $('#dynamic_field').append('<div class="row" id="row' + i + '"><div class="col-md-8"><ul class="categorylist"><li><input type="text" name="category[]" placeholder="' + i + '. Voeg een categorie toe" class="form-control name_list" /><div id="dynamic_questionfield' + i + '"><div class="row" id=questionrow"' + i + '">  <div class="col-md-8"> <ul class="questionlist">  <li> <input type="text" name="question[]" placeholder="1. Voeg een vraag toe" class="form-control name_list" />  </li> </ul>  </div>  <div class="col-md-4"> <button type="button" name="addquestion" id="addquestion' + i + '" class="btn btn-success questionbutton">Extra vraag</button>  </div> </div></div></li></ul></div><div class="col-md-4"><button type="button" name="remove" id="' + i + '" class="btn btn-danger btn_remove">X</button></div></div>');
});
$(document).on('click', '.btn_remove', function() {
  var button_id = $(this).attr("id");
  $('#row' + button_id + '').remove();
});

// Question
var e = 1;
$(document).on("click", "#addquestion" + i + "", function() {
  e++;
  $('#dynamic_questionfield' + i + '').append('<div class="row" id="questionrow' + e + '"> <div class="col-md-8"> <ul class="questionlist"> <li> <input type="text" name="question[]" placeholder="' + e + '. Voeg een vraag toe" class="form-control name_list"> </li> </ul> </div><div class="col-md-4"><button type="button" name="remove" id="' + e + '" class="btn btn-danger removequestion btn_removequestion">X</button></div></div>');
});
$(document).on('click', '.btn_removequestion', function() {
  var button_id = $(this).attr("id");
  $('#questionrow' + button_id + '').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dynamic_field">
  <div class="row">
    <div class="col-md-8">
      <ul class="categorylist">
        <li>
          <input type="text" name="category[]" placeholder="1. Voeg een categorie toe" class="form-control name_list" />
          <div id="dynamic_questionfield1">
            <div class="row">
              <div class="col-md-8">
                <ul class="questionlist">
                  <li>
                    <input type="text" name="question[]" placeholder="1. Voeg een vraag toe" class="form-control name_list" />
                  </li>
                </ul>
              </div>
              <div class="col-md-4">
                <button type="button" name="addquestion" id="addquestion1" class="btn btn-success questionbutton">Extra vraag</button>
              </div>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="col-md-4">
      <button type="button" name="addcategory" id="addcategory" class="btn btn-success">Extra categorie</button>
    </div>
  </div>
</div>

这是我的意思的屏幕截图:

当我单击第一个表示extra vraag的按钮时,它应该在第一个列表中添加一个额外的输入字段,但是将其添加到第二个列表中。

enter image description here

如果我添加多个列表,也会发生同样的情况,它总是将新的输入字段添加到最后添加的列表项中。

那是为什么?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试以下脚本。单击额外的vaarg按钮定位类“ questionbutton”时,它将基于您当前的html结构添加一个附加的行元素。

$(document).ready(function(){     var new_input = <div class="row"> <div class="col-md-8"> <ul class="questionlist"> <li> <input type="text" name="question[]" placeholder="1. Voeg een vraag toe" class="form-control name_list" /> </li> </ul> </div> <div class="col-md-4"> <button type="button" name="addquestion" id="addquestion1" class="btn btn-success questionbutton">Extra vraag</button> </div> </div>;

$(".questionbutton").on('click',function(){
    $(this).parent().parent().append(new_input);
});

});