仅使用JS打开特定模式

时间:2016-02-11 01:17:09

标签: modal-dialog

我找到了我想要的脚本(我是JS的初学者):https://stackoverflow.com/a/25060114/4857932

链接HTML

<li>
    {{ $comment->message }}

    @if (!$comment->children->isEmpty())
        @include("articles.comments.container", ['comments'=>$comment->children])
    @endif
</li>

模态JavaScript

<a href="#my_modal" data-toggle="modal" data-book-id="my_id_value">Open Modal</a>

但我想知道如何在不使用链接的情况下打开模态。只使用JS。那可能吗 ? 我尝试过这段代码,但它无效://triggered when modal is about to be shown $('#my_modal').on('show.bs.modal', function(e) { //get data-id attribute of the clicked element var bookId = $(e.relatedTarget).data('book-id'); //populate the textbox $(e.currentTarget).find('input[name="bookId"]').val(bookId); }); 你能救我吗?

感谢您的帮助! :)

1 个答案:

答案 0 :(得分:0)

首先,您必须说您正在使用Twitter Bootstrap框架。 你必须在模态内容::

上使用.modal - 方法
$('#my_modal_content').modal('show');
$('#my_modal_content').on('shown.bs.modal', function() {
  //get data-id attribute of the clicked element
  var bookId = $(e.relatedTarget).data('book-id');
  //populate the textbox
  $(e.currentTarget).find('input[name="bookId"]').val(bookId);
})