【问题标题】:Bootstrap dropdown on keyupkeyup 上的引导下拉菜单
【发布时间】:2016-11-19 21:58:02
【问题描述】:

我有一个在单击文本框时显示的有效下拉列表,但希望它始终显示在 keyup 上。我有 keyup 功能,但您仍然需要在键入后单击以显示下拉菜单。

        //This calls the delay function and will result in the customerNameUL_Populate function being called after the delay has passed.
        function customerNameSearchTextbox_keyup() {
            
            delay(function () {
                if ($('#customerNameSearchTextbox').val().length > 0) {
                    customerNameUL_Populate();
                    alert('dropdown');   //http://getbootstrap.com/javascript/#dropdowns
                    $('#customerNameSearchTextbox').dropdown(); <----- ?
                }
            else {
                    $('#customerNameUL').empty();
                };
            }, 500);
        };

    //This function gets called to populate the list of customers for search
        function customerNameUL_Populate() {

          <working stuff>
            
        };
        <div class="dropdown">
            <input type="text" id="customerNameSearchTextbox" class="form-control" placeholder="Customer" aria-describedby="basic-addon1" data-toggle="dropdown" onkeyup="customerNameSearchTextbox_keyup();" autocomplete="off">
            <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="customerNameUL">
            </ul>
        </div>

我认为我的困惑是要调用什么事件,或者要分配什么类,或者要调用什么事件。上面的警报显示我到达了我想去的地方,只需要替换下面的行以显示 customerNameUL。

【问题讨论】:

  • 我相信——这主要是一个简单的例子——你的问题主要是由于通过点击调用切换下拉的代码查找他们使用的数据切换属性。如果您要说,将数据切换添加到另一个未显示的元素,只需调用$().dropdown(),您就可以做您想做的事情

标签: jquery twitter-bootstrap dropdown


【解决方案1】:

我不得不更改一些代码,但这里的基础应该可以工作。如果您有任何问题,请告诉我。

function customerNameSearchTextbox_keyup() {
  var $this = $(this);
  //In jQuery, I use $this as a reference to the jQuery-wrapped this object
  var $parent = $this.parent('.dropdown');
  //Similar naming convention as $this. this is the parent object of our element, wrapped in jquery

  if ($this.val().length > 0 && !$parent.hasClass('open')) {
    //This if statement says:
    //"If this value's length is gerater than 0
    //AND the parent of this object does not have the 'open' class on it
    //Continue with this code block:
    $this.siblings('span.toggle-helper').dropdown('toggle');
    //searches for a sibling-level element that matches the CSS query 'span.toggle-helper', and fires the dropdown toggle method (showing it)
  } else if ($this.val().length == 0) {
    //If either one of those two requirements in the previous if is false, we end up here, where we only execute this code value's length is equal to 0.
    $('#customerNameUL').empty();
  };

};

$(function() {
  //$(function()....) is a shorter way to write $(document).ready(function()...);
  $('#customerNameSearchTextbox').on('keyup', customerNameSearchTextbox_keyup);
  //I did this because I was getting some weird sandbox errors leaving it as an 
  //attribute, but on an actual webpage (and not a snippet or fiddle) leaving 
  //this portion out and having the onkeyup="" attribute should be fine
})
.toggle-helper {
  display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="dropdown">
  <input type="text" id="customerNameSearchTextbox" class="form-control" placeholder="Customer" aria-describedby="basic-addon1" autocomplete="off" />
  <span class="toggle-helper" aria-haspopup="true" aria-expanded="false" data-toggle="dropdown">hi</span>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="customerNameUL">
    <li>Lorem ipsum.</li>
    <li>Doloremque, quam.</li>
    <li>Iure, vel!</li>
    <li>Culpa, rerum!</li>
  </ul>
</div>

【讨论】:

  • 我相信我明白你在那里做什么,我的问题是我在哪里调用 customerNameUL_Populate();进入你的函数 customerNameSearchTextbox_keyup()?
  • 这取决于。如果结果往往很快(你会比我更了解),那么我会说在代码开始时启动它(你的服务器端页面应该能够检查字符串是否为空并且很快什么都不返回),但实际上你可以在“if”代码块中启动它,然后从那里开始。因为我不知道那个代码是什么,所以很难使用它,所以我只是默认了一些选项。
  • 虽然也许您可以使用 websocket(我不知道您的结果是什么或它们将持续多长时间,因此最好在浏览器收到时一次将它们添加到列表中它比等待更长的时间并一次获得它们,到那时用户一直在输入)
  • 像魅力一样工作!非常感谢您对它发生的事情的帮助和详尽的解释!
猜你喜欢
  • 2012-07-05
  • 2015-08-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多