2

我有一个单列表,它的 TD 有一个 popover 类。因此,在该 TD 中,单击按钮时会要求指定输入,如下所示

在此处输入图像描述

如您所见,图表按钮触发了该 TD 的弹出框,并且弹出框上有 2 个输入。

所以我的方案是在弹出窗口中附加输入值以复制到“没有指定基准标签”的 div 中。那么如何确定弹出框是为哪个 TD 或哪个父级创建或启动的。当我点击保存按钮时,更清楚如何确定 Popover 的创建者。

代码

$('.main-attributes').popover({
    html: true,
    container: 'body',
    placement: 'auto top',
    trigger: 'manual',
    title: function () {


        return $(this).find('.attribute-title').html() + " - Score Range"
    },
    content: function () {

        var H = $('#div_scoreselector');

        return $(H).html();
    }
})
$('.manage-range').on('click', function (e) { //.manage-range is the chart button as shown in image which opens the popup
    var ma = $(this).parents('.main-attributes');
    $('.main-attributes').not(ma).popover('hide');
    $(ma).popover('toggle');
    e.stopPropagation();
});

分数选择器 DIV

<div class='hidden' id='div_scoreselector'>
    <div>
        <div class="row">
            <div class="col-md-12">
                <div class="row div-scorerange">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">
                                From</label>
                            <input placeholder="Low Score" type="text" class="form-control" />
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">
                                To</label>
                            <input placeholder="High Score" type="text" class="form-control" />
                        </div>
                    </div>
                </div>
                <hr />
                <div class="row">
                    <div class="col-md-6 col-md-offset-6">
                        <button class="btn-dark-grey btn">
                            CANCEL</button>
                        <button class="btn btn-red">
                            SAVE</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
4

2 回答 2

4

假设您以以下通用方式具有多个具有 main-attributes 类的元素:

<div class="main-attributes"> 
    <span class="text-container">No Benchmarks Specified</span>
    <button class="manage-range">Chart Button 1</button>
</div>
<div class="main-attributes"> 
    <span class="text-container">No Benchmarks Specified</span>
    <button class="manage-range">Chart Button 2</button>
</div>
... etc.

在您的 .manage-range 点击处理程序中设置对 $(this) 的引用(也就是在第 X 行单击的按钮),一旦触发了 Bootstrap 弹出框显示事件,您就可以将上下文作为数据值存储在 Save 按钮上。BS Popover 文档

$('.manage-range').on('click', function (e) {
    // ... your code here

    var $this = $(this);
    $('.main-attributes').off('shown.bs.popover').on('shown.bs.popover', function () {
        $('.popover button.save').data('context', $this);        
    });
    e.stopPropagation();
});

在您的 .manage-range 点击处理程序之外,为保存按钮设置一个点击事件,如下所示:

// Assuming you have a .save class on the Save button
$(document).on('click', '.save', function () {
    var $context = $(this).data('context');
    var fromval = $('.popover #fromvalue').val();
    var toval = $('.popover #tovalue').val();
    // Now get the text container relative to the $context passed in
    $context.siblings('.text-container').text('From: ' + fromval + ' To: ' + toval);
});

这是一个粗略的小提琴:http: //jsfiddle.net/9m8Yr/

于 2014-05-23T09:04:06.393 回答
0

很抱歉从坟墓中挖掘出来,但恕我直言,一个更优雅、更清洁的解决方案将是..

$('.main-attributes').popover({
    html: true,
    container: 'body',
    placement: 'auto top',
    trigger: 'manual',
    title: 'My Popover',
    content: function () {

        var H = $('#div_scoreselector');

        return $(H).html();
    }
})
$('.manage-range').on('click', function (e) { //.manage-range is the chart button as shown in image which opens the popup
    $(this).prev('.text-container').attr('id', 'mainattr'); //Add an ID to the previous .text-container
    var ma = $(this).parents('.main-attributes');
    $('.main-attributes').not(ma).popover('hide');
    $(ma).popover('toggle');
    e.stopPropagation();
});

$('.main-attributes').on('shown.bs.popover', function () {
    $('.save').click( function() {
        var fromval = $('.popover #fromvalue').val();
        var toval = $('.popover #tovalue').val();
           $('#mainattr').text('From: ' + fromval + ' To: ' + toval).removeAttr('id'); //Add the Values from Popover and then remove the ID we set.
        $('.main-attributes').popover('hide');
    });
});

http://jsfiddle.net/itsabhik/cxm4rt2u/2/

于 2015-08-16T16:51:53.277 回答