Bootstrap 模型每次显示相同的文本

Bootstrap model showing the same text each time

我正在使用 bootstrap 模式来显示弹出窗口:

<% @yourphotos.each do |photo| %>
  <!-- Modal -->
  <div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title"><%= name %></h4>
        </div>
        <div class="modal-body">
          <p>
          <% if photo.caption != nil %>
          <%= photo.caption.text %>
        <% else %>
          nil text
        <% end %>           
          </p>
        </div>
          <form class="navbar-form pull-left">
          <input type="text" class="span2">
          <button type="submit" class="btn">Comment</button>
        </form>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>

    </div>
  </div>
        </div>
    <% end %>

假设我遍历上面的内容并有 10 张照片(在@yourphotos 中)我的 caption.text 在我启动时的每个模态中都是相同的,即使当我查看源代码时我可以看到10 个不同的模态字幕..这是 JavaScript 的事吗?

如何打开模态?也许你一直在打开同一个模式?

尝试以下之一(对于 bootstrap 3)。它将在下次打开之前删除加载的数据。

$( document ).ready(function() {
    $("#myModal").on('hidden.bs.modal', function () {
        $(this).data('bs.modal', null);
    });
});

或者

$( document ).ready(function() {
    $('#myModal').on('hidden.bs.modal', '.modal', function () {
          $(this).removeData('bs.modal');
    });
});

或者

$( document ).ready(function() {
    $('#myModal').on('hidden.bs.modal', function () {
          $(this).removeData('bs.modal');
    });
});

Reference Link