0

'正在尝试根据对弹出窗口中显示的选项所做的选择来使用自定义数据属性值。

在下面的示例中,我有一条消息显示“我是单身,没有孩子”,点击单身时会出现一个带有选项(单身/已婚)的弹出框。如果用户点击已婚,那么应该被替换为We are MARRIED with no kids

小提琴

HTML

<div>
        <span id="m-marital-status" data-single="I am" data-multiple= "We are">I am</span>
        <div class="section-input">
            <div class="popover-markup" id="marital-status"><a href="#" class="trigger">Single</a> with 
                <div class="head hide"></div>
                <div class="content hide">
                    <ul>
                      <li>married</li>
                      <li>single</li>
                    </ul>
                    <div class="footer hide">test</div>
                </div>
                <span>no Kids</span>.
            </div>
        </div>

CSS

.popover-title {
    padding: 0;
    border: 0;
}

JS

$('.popover-markup>.trigger').popover({
    html: true,
    placement: 'top',
    content: function () {
        return $(this).parent().find('.content').html();
    }

});
4

3 回答 3

2

尝试

            <ul class="marital-status">
                <li data-status="We are">married</li>
                <li data-status="I am">single</li>
            </ul>

然后

$(document).on('click', 'ul.marital-status li', function () {
    $('#m-marital-status').css('color', 'red').html($(this).data('status'));
    $('#marital-status .trigger').text($(this).text())
    $('.popover-markup>.trigger').popover('hide')
});

演示:小提琴

于 2013-11-11T07:35:53.890 回答
1

您需要将 click 事件委托给 popover 内容,例如:

$('body').on("click", ".popover li", function() {
  // Code here
})

这是一个工作小提琴:http: //jsfiddle.net/h7fVL/3/

于 2013-11-11T07:35:44.887 回答
0

发生的情况是您将弹出窗口的 html 作为字符串传递给 Bootstrap。它稍后会创建另一个放置此内容的 div。这有两个问题。您正在尝试在显示弹出窗口之前附加事件。您的点击事件选择器实际上与为弹出窗口准备的 html 标记匹配,但与所示弹出窗口中的实际 DOM 元素不匹配。这是该问题的解决方案:

trigger.popover({
    html: true,
    placement: 'top',
    content: function () {
        setTimeout(function() {
            $('.popover-content li').click(function() {
                closePopup();
                $('#m-marital-status').css('color','red');
            });
        }, 500);
        return $(this).parent().find('.content').html();        
    }    
});

http://jsfiddle.net/h7fVL/4/

于 2013-11-11T07:47:22.573 回答