使用$('#MyModal')将数据传递给模态.modal('show');

时间:2016-11-07 16:34:00

标签: javascript jquery asp.net-mvc twitter-bootstrap bootstrap-modal

Dygraphs是一个插件,可以让您轻松地将json转换为HTML页面中的图表。使用dygraphs图表,我可以注释图表,注释可以有clickHandler事件:

annotations.push({
    series:  "@Model.Project.id_project",
    x: date,
    shortText: "X",
    text: message,
    cssClass: "annotation",
    clickHandler: function(annotation, point, dygraph, event) {
        $('#ChangeOrderModal').modal('show');  //<<<<<------------------------
    }
});

这一行:

$('#ChangeOrderModal').modal('show');

打开这个模式做得很好:

<div id="ChangeOrderModal" class="modal hide fade">
<div class="modal-body">
    This is a test
</div>
<div class="modal-footer">
    <a href="#" data-dismiss="modal" class="btn">Close</a>
</div>

但是,我需要将一些数据传递给这个模态,我不知道该怎么做。为简单起见,我们只是说它是一个字符串:

<div class="modal-body">
    This is a test.  String passed in: ______
</div>

从我打开模态的注释中,我将如何传入一个字符串?我将如何在模态中显示它?

谢谢!

1 个答案:

答案 0 :(得分:0)

在模态中添加一些元素来保存数据:

<div class="modal-body">
  This is a test. String passed in: <span id="passed-in-string"></span>
</div>

然后在显示模态之前简单地设置元素的值:

clickHandler: function(annotation, point, dygraph, event) {
  var stringToPass = 'Something';
  $('#passed-in-string').html(stringToPass);
  $('#ChangeOrderModal').modal('show');  //<<<<<------------------------
}