jquery - 如何在一个引导模式中绑定(bind)动态数据

标签 jquery bootstrap-modal

我的表单中有一个模式

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Image Tag Description</h4>
      </div>
      <div class="modal-body">
          <p id="desc">
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
      </p>
          <h2 id="cost">Cost : 20$</h2>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Edit</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

我有多个切换模式的按钮。 按钮的 HTML 如下

<button class="btn btn-primary tag_cnt" data-trigger="hover" 
        data-target="#myModal" data-toggle="modal" type="button" 
        style="top:144px; left:510px" value="1"></button>

我的表单中有多个按钮。现在我必须在每次单击按钮时调用相同的模态,并且我想在模态中动态显示数据。解决方案是什么?

最佳答案

您可以将动态变量传递给函数,然后在函数中您可以使用该变量“手动”打开模式:

<button class="btn btn-primary tag_cnt" style="top:144px; left:510px" onclick="showModal('data')" type="button" value="1"></button>

在您的 JavaScript 中,您可以像这样访问它:

function showModal(data)
{
   //you can do anything with data, or pass more data to this function. i set this data to modal header for example
   $("#myModal .modal-title").html(data)
   $("#myModal").modal();
}

关于jquery - 如何在一个引导模式中绑定(bind)动态数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27959117/

相关文章:

jquery - 想要图像翻转(第一个悬停状态)以在第二个悬停状态下与背景图像一起悬停

javascript - 有没有办法为每个 AJAX 页面加载初始化 JS SDK?

javascript - jQuery - 排序后更新可排序列表

javascript - 使用 ui-bootstrap 注入(inject)错误

php - Laravel中点击模态后如何获取标签的data-href值

javascript - 使用 Javascript 在 EditorFor MVC 中获得焦点

javascript - Underscore.js 中的 If 条件语句

javascript - 设置 css 元素的背景图像

javascript - 如何使用 jquery 滚动到模态内具有特定类的元素?

javascript - 如何重用克隆的元素?