简体   繁体   English

如何融入 <select>引导下拉列表中的元素?

[英]How to incorporate the <select> element within a bootstrap dropdown?

Fiddle 小提琴

The following fiddle allows a <select> dropdown element to be included as one of the <li> elements within a bootstrap dropdown. 以下小提琴允许<select>下拉元素作为引导下拉列表中的<li>元素之一包含在内。


The current problem within the fiddle is when the <select> dropdown element is clicked, it will disappear immediately, as well as the bootstrap dropdown, and not allow the user to select an option from the dropdown list. 小提琴中的当前问题是当单击<select>下拉元素时,它将立即消失,以及引导下拉列表,并且不允许用户从下拉列表中选择一个选项。 [Occurring in Chrome on Mac OS X] [在Mac OS X上的Chrome中发生]

Therefore, is it possible, within the attached fiddle, to allow the <select> element to work properly, allowing the user to select from the dropdown list, within the bootstrap? 因此,是否可以在附加的小提琴中允许<select>元素正常工作,允许用户从引导程序中的下拉列表中进行选择?

If an updated fiddle could please be provided, it would be extremely appreciated, as I am still new to coding. 如果可以提供更新的小提琴,我将非常感激,因为我还是新编码。

Thank You! 谢谢!

HTML HTML

<div class="dropdown btn-group">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
    Action
    <span class="caret"></span>
</a>
<ul class="dropdown-menu">
    <li><select data-toggle="dropdown" class="form-control" data-property="color">
            <option disabled>Select color:</option>
            <option>red</option>
            <option>green</option>
            <option>blue</option>
            <option>white</option>
            <option>black</option>
        </select></li>
</ul>

Add these lines of javascript to your fiddle 将这些javascript行添加到你的小提琴中

$('.dropdown-menu option, .dropdown-menu select').click(function(e) {
        e.stopPropagation();
});

Here is a fork 这是一个分叉

Take the data-toggle="dropdown" off of your select element. 从select元素中取出data-toggle="dropdown" Then add the class "preventDefault" to the li and add a function that stops propagation of events when that element is clicked like this: 然后将类“preventDefault”添加到li并添加一个函数,在单击该元素时停止事件的传播,如下所示:

 $(document).on('click', '.preventDefault', function(e) { e.stopPropagation(); e.preventDefault(); }); // add the below if selecting an option should close the dropdown $('.dropdown-menu option, .dropdown-menu select').change(function(e) { $('.dropdown.open .dropdown-toggle').dropdown('toggle').blur(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" /> <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script> <div class="dropdown btn-group"> <a class="btn dropdown-toggle" data-toggle="dropdown" href="#"> Action <span class="caret"></span> </a> <ul class="dropdown-menu"> <li class="preventDefault"> <select class="form-control" data-property="color"> <option disabled>Select color:</option> <option>red</option> <option>green</option> <option>blue</option> <option>white</option> <option>black</option> </select> </li> </ul> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM