【问题标题】:Selected item on Bootstrap dropdown listBootstrap 下拉列表中的选定项目
【发布时间】:2017-01-10 21:27:54
【问题描述】:

我有一个带有 Bootstrap 框架的网站。我有两个非常讨厌的输入字段,因为我无法让它们正常工作...

一个是这个 Bootstrap Typeahead 输入字段:

<input type="text" id="typeahead" name='name' placeholder='The name' class="form-control" data-provide="typeahead" autocomplete="off" />

另一个是这个下拉列表:

<div class="btn-group">
            <button class="btn btn-default dropdown-toggle" id="dates-dropdown-button" type="button" data-toggle="dropdown" href="#">
                <?=$reminder_table_th_date?>
                <span class="caret"></span>
              </button>
              <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="name-dates-dropdown">
              </ul>
</div>

(&lt;li&gt; 列表通过 Ajax 调用填充)

两者我都想做:如果我在 ajax 下拉字段中使用预先输入或使用下拉列表选择一个项目,则所选项目应该是该字段显示的值。我尝试了很多很多方法,例如:

How to Display Selected Item in Bootstrap Button Dropdown Title

Bootstrap 3 dropdown select

Bootstrap typeahead - How to set the default value manually

但它们都不起作用,我不知道为什么。我根本不是 JS/jQuery 大师,但这很可疑。我应该将这个函数的 jQuery 代码放在哪里以及如何放置?

PS:Firefox Firebug 没有显示任何 JS 错误,它们只是什么都不做,没有设置值或者没有调用它们的函数。

这是 typeahead Ajax 调用的 jQuery 代码,下拉列表(这很好用),还有一行应该设置 typeahead 值(在更新程序中),但遗憾的是,这不起作用:

<script type="text/javascript">

    $(document).ready(function() {
      $('#typeahead').typeahead({
        source: function (query, process) {
          $.ajax({
            url: '/functions/name-autocomplete.php',
            type: 'POST',
            dataType: 'JSON',
            data: 'query=' + query,
            success: function(data) {
              process(data);
            }
          });
        },
        displayText: function(item) {
            return item
        },
        updater: function (item) {
            $('#typeahead').typeahead('val', item);
            $.ajax({
            url: '/functions/name-dates-autocomplete.php',
            type: 'POST',
            dataType: 'JSON',
            data: 'query=' + item,
            success: function(data) {
              $('#name-dates-dropdown').html(data);
            }
          });
        }
      });
    });

</script>

【问题讨论】:

  • 所选项目应该是字段显示的值你能详细说明一下吗?
  • 当然。输入文本字段应将此设置为 VALUE,将下拉列表设置为选定项,因此将值替换为现在的一个 PHP 变量:=$reminder_table_th_date?>
  • 为了将
  • 元素设置为选中状态,我们需要识别该元素。返回的&lt;li&gt; 的样本会很有用
  • 2 月 27 日。
  • 但这与预输入文本或 &lt;?=$reminder_table_th_date?&gt; 有什么关系?我怎么能说“2 月 27 日”是我想要标记为选中的?
  • 标签: javascript jquery html twitter-bootstrap


    【解决方案1】:

    1。在 Bootstrap 下拉列表中显示所选项目

    为了处理引导下拉列表中的选择,您可以将点击事件处理程序绑定到 ul.dropdown-menu 元素。

    这使您也可以从嵌套的 li 元素中捕获点击事件,这实际上可以被视为选择事件。

    假设这个 html 结构:

    <div>
        <input type="text"
            id="typeahead"
            name="date"
            placeholder="date"
            class="form-control"
            data-provide="typeahead"
            autocomplete="off" />
    </div>
    
    <div class="btn-group">
        <button class="btn btn-default dropdown-toggle"
        id="dates-dropdown-button" type="button"
        data-toggle="dropdown" href="#">
            <!-- Placeholder for the selected th date -->
            <span class="selection"><?=$reminder_table_th_date?></span>
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="name-dates-dropdown">
            <li>12.11.97</li>
            <li>10.01.07</li>
            <li>2.11.2017</li>
        </ul>
    </div>
    

    然后您可以使用引导下拉列表处理选择 此事件处理程序:

    // handles selections on a bootstrap dropdown list
    $('.dropdown-menu').click(function (e) {
        var isLITag = e.target && e.target.tagName == 'LI';
        if (isLITag) {
            var selectedValue = $(e.target).text();
            $('#typeahead').typeahead('val', selectedValue);
            // Specifies the display value of the dropdown element
            $('.dropdown-toggle .selection').text(selectedValue);
            e.preventDefault();
        }
    });
    

    2。预输入值

    我认为如何将数据源分配给预输入存在问题,只有第二个参数接受datasources。另外typeahead.js提供了更丰富的数据源实现,叫做Bloodhound,值得考虑。

    我创建了一个使用 Bloodhound 数据源的演示。它还演示了如何指定 typeahead 的值以及如何处理 typeahead 选择。

    $(document).ready(function() {
    
      // Constructs the suggestion engine with a set of local dummy dates.
      var dateSet = {
        name: 'dates',
        display: 'date',
        source: new Bloodhound({
          datumTokenizer: Bloodhound.tokenizers.obj.whitespace('date'),
          queryTokenizer: Bloodhound.tokenizers.whitespace,
          local: [{
              'date': '12.11.97',
              'id': 0
            }, {
              'date': '2.11.2017',
              'id': 1
            }, {
              'date': '10.01.07',
              'id': 2
            }]
            /**
             * In order to integrate your ajax call
             * replace the local attribute with:
             *
             * remote: {
             *   url: '/functions/name-autocomplete.php?query=%QUERY',
             *   wildcard: '%QUERY'
             * }
             *
             * Twitter provides a list of examples at:
             *  http://twitter.github.io/typeahead.js/examples/
             *
             */
        })
      };
    
      // Creates the typeahead and assign the dateSet.
      $('#typeahead').typeahead({
        minLength: 1
      }, dateSet);
    
      // The selection handler sets the value for the drop down-menu
      // whenever a suggestion is chosen.
      $('#typeahead').bind('typeahead:select', function(ev, selection) {
        $('.dropdown-menu').val(selection.date);
        $('.dropdown-toggle .selection').text(selection.date);
      });
    
      // handles selections on a bootstrap dropdown list
      $('.dropdown-menu').click(function(e) {
        var isLITag = e.target && e.target.tagName == 'LI';
        if (isLITag) {
          var selectedValue = $(e.target).text();
          $('#typeahead').typeahead('val', selectedValue);
          //Specifies the display value of the dropdown element
          $('.dropdown-toggle .selection').text(selectedValue);
          e.preventDefault();
        }
      });
    });
    .tt-menu {
      background: white;
      box-shadow: 0 5px 10px rgba(0,0,0,.2);
      border-radius: 9px;
      padding: 10px;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/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://code.jquery.com/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
    <script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
      
    <div>
      <input type="text"
             id="typeahead"
             name='name'
             placeholder='Serach for ...'
             class="form-control"
             data-provide="typeahead"
             autocomplete="off" />
    </div>
    
    <div class="btn-group">
      <button class="btn btn-default dropdown-toggle"
              id="dates-dropdown-button" type="button"
              data-toggle="dropdown" href="#">
        <!-- Placeholder for the selected th date -->
        <span class="selection"><?=$reminder_table_th_date?></span>
        <span class="caret"></span>
      </button>
      <ul class="dropdown-menu" aria-labelledby="dropdownMenu1" id="name-dates-dropdown">
        <li>12.11.97</li>
        <li>10.01.07</li>
        <li>2.11.2017</li>
      </ul>
    </div>

    【讨论】:

    • 我将这段代码(在第 1 节中)放在了我的 的末尾 $(document).ready(function()) 但单击元素后仍然没有任何反应: (
    • 您是否在 button.dropdown-toggle 中放置了 =$reminder_table_th_date?> 元素?这是显示当前选择所必需的。
    • 是的,下拉列表现在看起来像这样:
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签