【问题标题】:Bootstrap Dropdown dynamic ajax list itemsBootstrap Dropdown 动态 ajax 列表项
【发布时间】:2015-05-13 21:05:07
【问题描述】:

我使用 Bootstrap 下拉菜单,并尝试在显示下拉菜单之前通过 AJAX 动态填充列表项。我的脚本必须从数据库中获取可用项目并将它们插入到

    我阻止默认事件,执行 ajax 并在之后触发,但 "data" 未定义,我没有看到下拉菜单。像这样的脚本通常适用于表单。

        $(".dropdown").on("show.bs.dropdown", function(e, data){
        ths = $(this);
        if (data == 'okay') return true;
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/core/test.php',
            data: 'test'
        }).done(function(response) {
            $(ths).children('ul').html(response);
            $(ths).trigger('click.bs.dropdown', 'okay');
        });
    
    });
    

    html:

    <div class="dropdown">
       <a data-toggle="dropdown" 
          class="btn dropdown-toggle">My list <b class="caret"></b>
       </a>
       <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
          ...data from ajax puts here...
       </ul>
    </div>
    

    我该如何解决?谢谢。

    【问题讨论】:

    • 下拉菜单是否有data 参数和事件?我不认为它有

    标签: javascript jquery html ajax twitter-bootstrap


    【解决方案1】:

    引导事件不传递参数,并且在您的代码中您期望edata
    您应该检查 ul 是否已经有孩子,如果它没有获取一些并附加它们。

    【讨论】:

    • 感谢您的回答。
    猜你喜欢
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    • 2010-12-16
    相关资源
    最近更新 更多