如何在bootstrap模式中传递动态数据?

时间:2016-12-06 19:38:19

标签: javascript jquery twitter-bootstrap

我正在努力实现以下目标:

我有一个按钮:

<button id= "<dynamicid>" type="button" class="btn btn-danger btn-sm pull-right" data-toggle="modal" data-target="#mymodal">Open</button>

每次都会更改id属性。现在单击此按钮可打开模态。我想要实现的是将这个动态id放入打开的模态中。

这是完整的模态:

<div class="modal fade" id="mymodal" data-placement="right" role="dialog">
    <div class="modal-dialog">
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header" style="padding: 4%; background-color: white; color: #6A6A6A; font-family: 'Crete Round', serif;">
        <input type="submit" class="btn btn-primary btn-sm" value="submit"/>
        <div class="modal-footer">
        </div>
      </div>      
    </div>
</div>
</div>

对此的任何建议都会有很大的帮助。

1 个答案:

答案 0 :(得分:0)

由于您没有真正具体说明您要对ID进行的操作,请参阅以下示例:

$('#mymodal').on('show.bs.modal', function(e) {
    $(this).find('.modal-body').text(e.relatedTarget.id);
});

Example

参考:

bootstrap - modal events

&#13;
&#13;
$('#mymodal').on('show.bs.modal', function(e) {
  var id = $(this).find('.modal-body').text(e.relatedTarget.id);
  if(id) console.log(id);
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<button id="whateverID" type="button" class="btn btn-danger btn-sm pull-right" data-toggle="modal" data-target="#mymodal">Open</button>
<div class="modal fade" id="mymodal" data-placement="right" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header" style="padding: 4%; background-color: white; color: #6A6A6A; font-family: 'Crete Round', serif;">
        Header

      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;