Jquery事件委派不起作用

时间:2013-11-13 03:32:10

标签: jquery html5 twitter-bootstrap

我正在尝试根据在相应的popovers上点击/输入的值更新我的div值。

我有一条消息“我是单身,没有孩子”,点击单个弹出框出现选项(单身/已结婚)。如果用户点击已婚,那么我应该被替换为我们已婚,没有孩子JSFIDDLE的作品。

当我尝试添加一行时,用户在弹出框中输入age并单击OK,标签中的文本应该更新。这是fiddle。我尝试了这个机智事件代表团..它无法正常工作

@Pete - 我尝试使用data-btn自定义属性,因为我将重新使用'.popover-content button'。我修改了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
    }
});

2 个答案:

答案 0 :(得分:2)

@Trevor是正确的,因为您的选择器不正确。 .popover函数会将弹出窗口附加到body元素(有一个container参数,可以让您覆盖but it is apparently non-functional)。

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

您的处理程序中还存在其他一些问题(在您未定义event.target时调用event开始)但是它们已经很容易纠正。

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

答案 1 :(得分:1)

所以我将您的.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/

相关问题