[英]Bootstrap dropdown with jquery
I have a form, and I'm trying to use a dropdown as a message displayer. 我有一个表单,我正在尝试使用下拉列表作为消息显示器。 When I focus out the input, the dropdown displays.
当我将输出聚焦时,显示下拉列表。 The problem is that it closes right after showing.
问题是它在显示后立即关闭。
<form method="post">
<input id="name" type="text" name="name">
<li class="dropdown list-unstyled">
<a class="dropdown-toggle" role="button"data-toggle="dropdown"></a>
<ul class="dropdown-menu">
My message.
</ul>
</li>
</form>
Jquery jQuery的
$(document).ready(function (){
$('#name').focusout(function () {
$('.dropdown-toggle').dropdown('toggle');
});
});
I couldn't figure out why it closes yet. 我无法弄清楚为什么它会关闭。 Strange thing is that if I click outside and drag the mouse just a little before releasing it, the dropdown doesn't close.
奇怪的是,如果我点击外面并在释放它之前拖动鼠标一点,下拉菜单就不会关闭。 Thanks!
谢谢!
fiddle https://jsfiddle.net/syp7ynqm/ 小提琴https://jsfiddle.net/syp7ynqm/
edit: the problem seems to be that the dropdown detects the outside click right after it shows, so it closes (as it should) but I would like to disable this function, only for this first click, so that on the focus out the message would be displayed correctly. 编辑:问题似乎是下拉列表在显示后立即检测到外部点击,因此它关闭(应该如此)但我想禁用此功能,仅用于此第一次点击,以便在焦点上显示消息会正确显示。
You can just go with the show method. 你可以使用show方法。 Fiddle .
小提琴 。
$(document).ready(function (){
$('#name').focusout(function () {
$('.dropdown-menu').show();
});
});
And your html should look like the following because li should be a child of ul so you would want to go with the folling html. 你的HTML应该如下所示,因为li应该是ul的孩子,所以你想要使用folling html。
<form method="post">
<input id="name" type="text" name="name">
<div class="dropdown">
<div class="dropdown-menu">
My message.
</div>
</div>
</form>
$(document).ready(function (){ $('#name').focusout(function () { $('.dropdown-menu').toggle(); }); $(document).mousedown(function(){ $('.dropdown-menu').hide(); }) });
Instead of the dropdown method use toggle to show or hide the dropdown. 而不是下拉方法使用切换来显示或隐藏下拉列表。
$(document).ready(function (){
$('#name').focusin(function(){
$('.dropdown-menu').toggle();
});
$('#name').focusout(function(){
$('.dropdown-menu').toggle();
});
});
This results in the dropdown showing up when the input is focused and disappearing when you click away. 这会导致在输入聚焦时显示下拉列表并在您单击时消失。
Check this fiddle : https://jsfiddle.net/e59xb38p/40/ 检查这个小提琴: https : //jsfiddle.net/e59xb38p/40/
$(document).ready(function (){
var $dropDownMenu = $('.dropdown-menu');
// displays as default
$dropDownMenu.css("display", "block");
$('#name')
.focusin(function () {
$dropDownMenu.css("display", "none");
})
.focusout(function () {
$dropDownMenu.css("display", "block");
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.