将bootstrap popover输入值绑定到拥有该popover的元素

时间:2014-05-23 05:57:59

标签: jquery twitter-bootstrap-3 bootstrap-popover

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

enter image description here

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

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

CODE

$('.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>

2 个答案:

答案 0 :(得分:3)

假设您使用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 popover显示事件,您就可以将上下文存储为数据值。保存按钮。 BS Popover docs

$('.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/

答案 1 :(得分: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/