【问题标题】:Use one modal bootstrap for multiple button对多个按钮使用一个模式引导程序
【发布时间】:2018-04-16 00:02:33
【问题描述】:

我从 json 中获取数据并使用 html 将数据添加到我的页面中,如下所示:

$.ajax({
  url: url,
  type: "post",
  dataType: "json",
  data: {
    idbv: 1
  },
  success: function(data) {
    $.each(data, function(index, value) {
      var id = value.id;
      var content = value.content;
      $("#test").append('<button id="btn_'+id +'"></button><div id="modal_'+id+'" class="modal fade" role="dialog"> <div class="modal-dialog"> <div class="modal-content"> <span>'+content+'</span> </div> </div> </div>');
      //This is the old way i do, it's so bad
      $("#btn_"+id).click(function(){
        $("#modal_"+id).modal("show");
      });
    }
  });
});

如何打开模态引导程序并将数据从 json 发送到具有一个模态的模态引导程序?

【问题讨论】:

标签: javascript jquery json twitter-bootstrap


【解决方案1】:

Working fiddle.

只需复制您附加到模式内测试元素的数据并显示如下:

$('#myModal .modal-body').html( $('#test').html() );
$('#myModal').modal();

完整代码:

$.ajax({
  url: url,
  type: "post",
  dataType: "json",
  data: {
    idbv: 1
  },
  success: function(data) {
    $.each(data, function(index, value) {
      var id = value.id
      $("#test").append('<button id="btn_'+id +'"></button>');
    });

    $('#myModal .modal-body').html( $('#test').html() );
    $('#myModal').modal();
  }
});

希望得到他的帮助。

【讨论】:

  • 那么,请再次查看我的问题,如何将内容发送到模态?
  • 请添加您的 HTML 代码,以便我们看到您在说的是哪种模式。
  • 让我检查一下
猜你喜欢
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 2021-02-20
相关资源
最近更新 更多