【问题标题】:Jquery event delegation not workingJquery事件委托不起作用
【发布时间】:2013-11-13 03:32:09
【问题描述】:

我正在尝试根据在相应弹出框上单击/输入的值更新我的 div 值。

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

当我尝试再添加一行时,用户在弹出窗口中输入年龄并单击“确定”,标签中的文本应该会更新。这里是fiddle。我试过这个机智的事件委托..它不起作用

@Pete -- 我尝试使用 data-btn 自定义属性,因为我将重新使用“.popover-content 按钮”。我修改了UPDATED FIDDLE 似乎没有用..有什么见解吗?

HTML

<div id="ln1">
    <span data-multiple="We are" data-single="I am" id="m-marital-status">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 class="marital-status">
                    <li data-sataus="We are">married</li>
                    <li data-sataus="I am">single</li>
                </ul>
            </div>
            <span>no Kids</span>.
        </div>
    </div>
</div>
<div id="ln2">
    I am
    <div class="section-input">
        <div class="popover-markup" id="my-age"><a href="#" class="trigger">35</a>
            <div class="head hide"></div>
            <div class="content hide">
                <div class="form-group">
                    <input class="form-control" placeholder="0" type="text"/>
                </div>
                <button class="btn btn-default btn-block" type="button">ok</button>
            </div>
            <span>no Kids</span>.
        </div>
    </div>
    <span class="hide" id="spouse-info">
        and my spouse is
        <span class="white">32</span>
    </span>.
</div>

Js

$status = $("#m-marital-status")
$('.popover-markup>.trigger').popover({
    html: true,
    placement: 'right',
    content: function () {
        return $(this).parent().find('.content').html();
    }

});
$('body').on("click", ".popover-markup li, .popover-markup button", function () {

    event.preventDefault();
    var target = event.target;

    switch (target.tagName.toLowerCase()) {
    case '.popover-markup li':
        $('.popover-markup>.trigger').popover("hide");
        if ($(this).text() == "married") {
            $status.text($status.data("multiple"));
            $('#marital-status .trigger').text($(this).text());
            $('#spouse-info').removeClass('hide');

        } else {
            $status.text($status.data("single"));
            $('#marital-status .trigger').text($(this).text());
            $('#spouse-info').addClass('hide');
        }
        break;
    case '.popover-markup button':
        var age = $(this).closest('input').val();
        $(this).closest('.popover-markup a').text(age);
        break;

    default:
        // do nothing
    }
});

【问题讨论】:

    标签: jquery html twitter-bootstrap


    【解决方案1】:

    @Trevor 是正确的,因为您的选择器不正确。 .popover 函数将弹出框附加到 body 元素(有一个 container 参数应该允许您覆盖它,but it is apparently non-functional)。

    因为弹出框附加到正文中,所以它不是.popover-markup 的后代。将选择器更改为 .popover-content li.popover-content button 可以很好地找到它们并进行委托。

    您的处理程序中还有一些其他问题(从调用 event.target 开始,当您没有定义 event 时),但它们很容易得到纠正。

    工作演示:http://jsfiddle.net/R28sQ/10/

    【讨论】:

    • 我尝试使用 data-btn 自定义属性,因为我将重新使用“.popover-content 按钮”。我修改了小提琴jsfiddle.net/R28sQ/12 似乎没有用..有什么见解吗??
    • 可以使用自定义属性,但需要先定义。您只需将data-btn="my-age" 添加到按钮标记或在$('.popover-markup &gt; .trigger') 的单击处理程序中添加$('.popover-content button').data('btn', 'my-age');(我的建议是标记)。查看更新的小提琴:jsfiddle.net/R28sQ/14
    【解决方案2】:

    所以我将您的 .popover-markup button 更改为 .popover-content button,因为我无法在您的按钮上看到该类。我认为这就是为什么您的事件没有触发的原因,因为您的点击功能上有错误的类。

    $('body').on("click", ".popover-markup li, .popover-content button", function() {
    

    而且我必须对您获得价值的方式进行一些更改。

    case 'button':
                 var age = $(this).parent().find('input').val();
                 $(this).closest('.popover').prev('div').find('.popover-markup a').text(age);
                 $('.trigger').popover('hide');  // If you want to hide the popover when clicking okay.
                 break;
    

    小提琴示例

    http://jsfiddle.net/R28sQ/3/

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 2014-01-19
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多