【问题标题】:Creating a bootstrap popover on click of a button with dynamic content and dismiss when click again在单击具有动态内容的按钮时创建引导弹出窗口,并在再次单击时关闭
【发布时间】:2014-02-21 11:44:27
【问题描述】:

我有一个页面,其中有一个 jquery 网格和一个带有和图标的链接。我需要显示一个弹出框并在弹出框内显示一个表单。但是当用户点击另一个链接(应该出现另一个弹出框)或页面上除弹出框之外的任何其他位置时,弹出框应该关闭。

我写的当前代码不起作用。

当前代码:

链接的HTML代码(这个div是网格中的一列,每一行的rowid都会不同):

<div rowid="5" class="action-buttons">
    <a href="javascript:void(0);" class="blue editRow">
       <i class="icon-pencil bigger-130"></i>
    </a>
    <a href="javascript:void(0);" class="green markComplete">
       <i class="icon-check bigger-130"></i>
    </a>
</div>

Javascript 代码:

$('#grid-table').on('click','.editRow',function(e){
    $(this).popover({
        html: true,
        title: 'Popover Title<a class="close" href="#">&times;</a>',
        content: '<a>This one works fine!!</a>' //Content can change based on rowId
    });
    $(this).popover('show');
    e.stopPropagation();
    $('body').click(function (e) {
        if (($('.popover').has(e.target).length == 0) || $(e.target).is('.close')) {
            $(this).popover('hide');
        }
    });
});

【问题讨论】:

  • 能否请您为此创建一个fiddle

标签: javascript jquery html twitter-bootstrap


【解决方案1】:

您应该在.editRow点击事件之外编写以下代码:

$('body').click(function (e) {
        if (($('.popover').has(e.target).length == 0) || $(e.target).is('.close')) {
            $(this).popover('hide');
        }
    });

【讨论】:

  • 实际上网格表 div 是使用 ajax 调用加载的,可能会出现 .popover 或 .editRow 届时不可用的情况。将此代码放在 .editRow 点击事件之外后,它甚至没有显示弹出框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 1970-01-01
相关资源
最近更新 更多