[英]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下拉列表并将其样式设置为类似于按钮。
<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.