【问题标题】:Twitter Bootstrap dropdown misbehaviorTwitter Bootstrap 下拉错误行为
【发布时间】:2012-12-08 01:33:51
【问题描述】:

我有两个下拉列表,一个有 id,另一个没有,我想触发一个 ajax 请求和显示下拉列表的操作。 这就是我尝试这样做的方式,但是有一些不当行为,在这种情况下,它触发了 ajax 请求,但是当显示下拉列表时,它同时显示了两个下拉列表。

          <li class="dropdown top-dropdown">
            <a href="#" class="dropdown-toggle" style="padding-right:0;" data-toggle="dropdown">hola</a>
            <ul class="dropdown-menu">
              <li>
                <a href="/hola"><i class="icon-user"></i>
                  Profile
                </a>
              </li>
              <li>
                <a href="/hola"><i class="icon-wrench"></i>
                  Settings
                </a>
              </li>              
            </ul>
          </li>

          <li id="hola" class="dropdown">
            <a id="go_do_something" href="<%= url %>" class="dropdown-toggle disabled" style="padding-right:0;" data-toggle="dropdown">           
              <b id="noti_container">
                <i class="icon-bullhorn"></i>
              </b>
            </a>
            <ul class="dropdown-menu">              
                <li>
                  <a href="/hola">
                    <i>added on yesterday</i>
                  </a>
                </li>              
              <li class="divider"></li>
              <li>
                <a href="#">
                  See All
                </a>
              </li>
            </ul>  
          </li>

$("#go_do_something").live("click", function(){      
    var update_user_last_n = $(this).attr('href');

    $.ajax({
      type: 'GET',
      url: update_user_last_n
    }).done(function(xhr, status) {            
      $("#hola").dropdown('toggle');
    }).fail(function(xhr, status) {

    });  

  });

在其他情况下,我尝试使用 data-target 属性而不是 href,如 Bootstrap Docs 中的描述,但我在浏览器上遇到错误:

Uncaught Error: Syntax error, unrecognized expression: {url_on_data-target}

【问题讨论】:

    标签: jquery jquery-selectors twitter-bootstrap twitter-bootstrap-rails


    【解决方案1】:

    有一种优雅的方法可以做到这一点,但这里有一个解决问题的解决方法:

      $("#go_do_something").live("click", function(event){
        event.preventDefault();  
        var this_a = this;  
        var url = $(this_a).attr('href');
    
        $.ajax({
          type: 'GET',
          url: url
        }).done(function(xhr, status) {                  
          $(this_a).removeClass("disabled");
          $(this_a).click();
        }).fail(function(xhr, status) {
    
        });  
    
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-23
      • 2012-02-21
      • 1970-01-01
      • 2023-03-03
      • 2014-01-25
      • 1970-01-01
      • 2012-04-03
      • 2012-03-04
      相关资源
      最近更新 更多