【问题标题】:dynamic Accordion post and answer动态手风琴帖子和答案
【发布时间】:2017-08-06 07:16:58
【问题描述】:

我有这个小提琴

http://jsfiddle.net/nesreen/m5TMF/763/

$("#AddansId").click(function(){
//get the parent of the div with id(Collapse+x)
// to abend the data content of the textarea beside the word
//Account Registeration at PrepBootstrap
}); 

我可以用按钮创建一个动态手风琴 在每个手风琴内我需要制作另一个文本区域和按钮以在同一个折叠中添加另一个文本 但我面临的问题是,即使父 id 也是动态的,我如何才能每次都获取按钮的父

【问题讨论】:

  • 您的问题不清楚..能否请您逐步详细说明您面临的问题??
  • 希望对您有所帮助! fiddle

标签: javascript jquery html twitter-bootstrap model-view-controller


【解决方案1】:

让我们看看。在这里采取的几个步骤:

  • 在这种情况下,首先您需要以不同的方式绑定点击(event delegation),因为您想动态点击 添加元素

  • 其次,我们使用closest()方法和attr('id')获取父id

  • 要获得 textarea 值,您可以使用 $(this).parent().prev().find('textarea').val()

  • 然后使用append()方法将文本附加到您想要的单词旁边,最后使用$(this).closest('.container').append(answer);在单词前面添加文本(这有点不清楚所以我在Account Registeration at PrepBootstrap之前添加了它。我希望这就是你的意思)

var x = 1;
$(document).on('click', "#AddansId", function() {
  var idOfParent = $(this).closest('.panel-collapse').attr('id');
  //console.log(idOfParent);
  var answer = $(this).parent().prev().find('textarea').val();
  $(this).closest('.container').append(answer);
});

$(document).on('click', '#AddQuestId', function() {
  x = x + 1;
  $("#accordion").append('<div class="panel panel-default">' +
    '<div class="panel-heading">' +
    '<h4 class="panel-title"> ' +
    '<a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapse' + x + '">' + $("#comment").val() + '</a>  </h4>' +
    '</div>' +
    '<div id="collapse' + x + '" class="panel-collapse collapse in">' +
    '<div class="panel-body">' +
    '<div class="container">  ' +
    ' <form>' +
    '<div class="form-group" >' +
    '<label for="comment">answer:</label>' +
    '<textarea class="form-control" rows="5" id="answer' + x + '"></textarea> ' +
    '  </div>' +
    ' <div style="float: right;"> <input type="button" class="btn btn-info" value="answer" id="AddansId"></div>' +

    '</form>' +
    '</div>' +
    'Account registration at <strong>PrepBootstrap</strong>' +
    '</div>' +
    '</div>' +
    '</div>' +
    '</h4>' +
    '</div>');
});
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>

<div class="container">
  <form>
    <div class="form-group">
      <label for="comment">Comment:</label>
      <textarea class="form-control" rows="5" id="comment"></textarea>
    </div>
    <div style="float: right;"> <input type="button" class="btn btn-info" value="Add Question" id="AddQuestId"></div>

  </form>
</div>

<div class="panel-group" id="accordion">
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多