简体   繁体   English

当用户从下拉菜单中选择项目时,如何防止引导程序模式关闭

[英]How to prevent a bootstrap modal from closing when user selects item from dropdown-menu

In an example below, when user clicks on any item from dropdown list, then the modal window is automatically trigerred to close. 在下面的示例中,当用户单击下拉列表中的任何项目时,模式窗口将自动触发以关闭。 The modal window shall close when user clicks on SEND button. 当用户单击“发送”按钮时,模式窗口将关闭。 Category and message are required to activate the SEND button (but this is out of topic). 必须使用类别和消息才能激活“发送”按钮(但这不属于主题)。

I tried to prevent that behaviour by 我试图通过防止这种行为

$('.dropdown-menu').click(function(event){
    event.stopPropagation();
});​

,but it doesn't change anything. ,但没有任何改变。

<div class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <form role="form">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="dropdown">
                                <button id="dLabel" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Select category<span class="caret"></span></button>
                                <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
                                    <li><a href="">Category 1</a></li>
                                    <li><a href="">Category 2</a></li>
                                    <li><a href="">Category 3</a></li>
                                    <li><a href="">Category 4</a></li>
                                    <li><a href="">Category 5</a></li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="form-group">
                                <label for="message-text" class="control-label">MESSAGE</label>
                                <textarea class="form-control" id="message-text"></textarea>
                            </div>
                        </div>
                    </div>
                </form>                     
                <div class="row">
                    <div class="col-xs-2">
                        <p><a class="btn btn-primary" href="#" role="button">SEND</a></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

You're using a dropdown meant for the navbar, which will cause an action to take place the moment it's clicked because it's intended to send people to a URL. 您正在使用一个用于导航栏的下拉菜单,这将导致单击该按钮后立即执行操作,因为该操作旨在将人们发送到URL。 Instead, use a standard HTML dropdown and style it to look like a button. 而是使用标准HTML下拉列表并将其样式设置为类似于按钮。

BOOTPLY 引导

<div class="modal fade" id="myModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <form role="form">
                    <div class="row">
                        <div class="col-xs-12">
                            <select class="btn btn-default">
                              <option value="1">Option 1</option>
                              <option value="2">Option 2</option>
                              <option value="3">Option 3</option>
                              <option value="4">Option 4</option>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="form-group">
                                <label for="message-text" class="control-label">MESSAGE</label>
                                <textarea class="form-control" id="message-text"></textarea>
                            </div>
                        </div>
                    </div>
                </form>                     
                <div class="row">
                    <div class="col-xs-2">
                        <p><a class="btn btn-primary" href="#" role="button">SEND</a></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

I assume you want the selected action to be used for something, and as a result you'll want that action to be displayed to the user once they select it to keep things obvious. 我假设您希望所选的动作用于某件事,因此,一旦用户选择该动作以使事情变得显而易见,便希望该动作显示给用户。 Further, using an actual dropdown means you can easily pull out the selected value when the user hits the submit button. 此外,使用实际的下拉列表意味着您可以在用户点击提交按钮时轻松地拉出所选值。 Your button is also a stylized anchor tag, so it's not going to work the way a standard button would, but I don't know your entire use case. 您的按钮也是样式化的锚标记,因此不会像标准按钮那样工作,但我不知道您的整个用例。 Just pointing it out to cover all my bases. 只是指出来涵盖我的所有基地。

You are using links without a value in href . 您正在使用href没有值的链接。 Add '#' or any other value. 添加“#”或任何其他值。

<ul class="dropdown-menu" role="menu">
    <li><a href="#">Action</a></li>
    <li><a href="#">Another action</a></li>
    <li><a href="#">Something else here</a></li>
    <li class="divider"></li>
    <li><a href="#">Separated link</a></li>
</ul>

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

相关问题 Bootstrap,如何防止显示模式弹出窗口时关闭下拉菜单 - Bootstrap, how to prevent from closing a dropdown-menu when modal popup displayed React bootstrap 防止下拉菜单在下拉项目选择时关闭 - React bootstrap prevent dropdown menu from closing on dropdown item select 从生成的引导程序下拉菜单中查找下拉项 - Find dropdown-item from generated bootstrap dropdown-menu 当用户刷新整个页面时,阻止bootstrap 3模态关闭 - prevent bootstrap 3 modal from closing when user refreshes the entire page Bootstrap 4:单击下拉项时如何关闭下拉菜单? - Bootstrap 4: How Do I Close a dropdown-menu when a dropdown-item is clicked? 当用户从下拉菜单中选择一个选项时,如何更新任何归档的文本? - How to update any text filed when the user is select an option from a dropdown-menu? 完成浏览器刷新后,如何防止Bootstrap模式关闭? - How to prevent the Bootstrap modal from closing when browser refresh is done? 如果我单击菜单项,防止下拉菜单关闭 - Prevent Dropdown menu from closing if I click a menu item 如何防止 Bootstrap Modal 自动关闭? - How to prevent the Bootstrap Modal from auto closing? 子菜单中的Bootstrap下拉菜单取消悬停 - Bootstrap dropdown-menu unhover when in submenu
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM