我有2个文本输入字段,每个文本输入字段都有一个关联的隐藏div。 当客户专注于每个文本字段时,将显示隐藏的div。如果客户在文本字段或关联的div以外的任何位置单击/聚焦,则它们都将消失(一次只能看到1个隐藏的div)。
我找到了working example snippet for a single text field input,但是我不知道如何为两个单独的输入/ div调整脚本。
1)“搜索市场”中的焦点显示“选择市场” div(暗区单击将关闭/隐藏div)。
2)“搜索符号”中的焦点显示“选择符号” div(深色区域单击将关闭/隐藏div)。
//BASED OFF SO SINGULAR EXAMPLE
//https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat#answer-2427363
$('#search-markets').focus(function() {
$('div.select-filters').css('display', 'flex');
$(document).bind('focusin.select-filters click.select-filters',function(e) {
if ($(e.target).closest('.select-filters, #search-markets').length) return;
$(document).unbind('.select-filters');
$('div.select-filters').slideUp(300);
});
});
$('div.select-filters').hide();
#select-data-inputs {
background-color: #000;
}
.select-filters {
background-color: rgba(0, 0, 0, 0.8);
border-top: 2px solid #fff;
color: #fff;
}
#select-symbols {
background-color: rgba(1, 56, 89, 0.85);
}
#select-markets {
background-color: rgba(2, 104, 165, 0.85);
}
.filter-list li.list-inline-item {
width: 48%;
margin: 0;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<div class="container-fluid">
<div class="row m-5 ">
<div class="col-12 text-center">
<h1>On form inputs focus, show div. hide div on blur for <span class="text-danger">multiple inputs</span> and hidden divs</h1>
<p class="lead"><i><a href="https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat" target="_blank">Based from SO sigular example</a></i></p>
</div>
</div>
<div class="row">
<!--TEXT FIELDS INPUT ROW-->
<!--TEXT FIELDS INPUT ROW-->
<div id="select-data-inputs" class="controls form-row p-3 w-100">
<div class="col-4">
<input type="text" id="search-markets" class="input form-control" placeholder="Search Markets">
</div>
<div class="col-4 offset-1">
<input type="text" id="search-symbols" class="input form-control" placeholder="Search Symbols">
</div>
</div>
</div>
<div id="main-display">
<!--HIDDEN DIV FOR FIRST TEXT FIELD-->
<!--HIDDEN DIV FOR FIRST TEXT FIELD-->
<div id="select-markets" class="row select-filters p-4">
<div class="select-heading col-12 pl-2">
<h6 class="small-sub-heading">Select markets</h6>
</div>
<div class="col-4 pt-2 select-filter-items">
<ul class="filter-list list-unstyled pl-2">
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-1" value="market-option-1">
<label class="form-check-label" for="market-option-1">Market Option 1</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-2" value="market-option-2">
<label class="form-check-label" for="market-option-2">Market Option 2</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-3" value="market-option-3">
<label class="form-check-label" for="market-option-3">Market-Option 3</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-4" value="market-option-4">
<label class="form-check-label" for="market-option-4">Market-Option 4</label>
</li>
</ul>
</div>
</div>
<!--HIDDEN DIV FOR SECOND TEXT FIELD-->
<!--HIDDEN DIV FOR SECOND TEXT FIELD-->
<div id="select-symbols" class="row select-filters p-4">
<div class="select-heading col-4 offset-5 pl-2">
<h6 class="small-sub-heading">Select symbols</h6>
</div>
<div class="col-4 offset-5 pt-2 select-filter-items">
<ul class="filter-list list-unstyled pl-2">
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-1" value="symbol-option-1">
<label class="form-check-label" for="symbol-option-1">Symbol Option 1</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-2" value="symbol-option-2">
<label class="form-check-label" for="symbol-option-2">Symbol Option 2</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-3" value="symbol-option-3">
<label class="form-check-label" for="symbol-option-3">Symbol Option 3</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-4" value="symbol-option-4">
<label class="form-check-label" for="symbol-option-4">Symbol Option 4</label>
</li>
</ul>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>
答案 0 :(得分:0)
我想您想要的是这样的东西
//BASED OFF SO SINGULAR EXAMPLE
//https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat#answer-2427363
$('#search-markets').focus(function() {
$('div.select-filters.market').css('display', 'flex');
$(document).bind('focusin.select-filters.market click.select-filters.market',function(e) {
if ($(e.target).closest('.select-filters.market, #search-markets').length) return;
$(document).unbind('.select-filters.market');
$('div.select-filters.market').slideUp(300);
});
});
$('#search-symbols').focus(function() {
$('div.select-filters.symbol').css('display', 'flex');
$(document).bind('focusin.select-filters.symbol click.select-filters.symbol',function(e) {
if ($(e.target).closest('.select-filters.symbol, #search-symbols').length) return;
$(document).unbind('.select-filters.symbol');
$('div.select-filters.symbol').slideUp(300);
});
});
$('div.select-filters').hide();
#select-data-inputs {
background-color: #000;
}
.select-filters {
background-color: rgba(0, 0, 0, 0.8);
border-top: 2px solid #fff;
color: #fff;
}
#select-symbols {
background-color: rgba(1, 56, 89, 0.85);
}
#select-markets {
background-color: rgba(2, 104, 165, 0.85);
}
.filter-list li.list-inline-item {
width: 48%;
margin: 0;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<div class="container-fluid">
<div class="row m-5 ">
<div class="col-12 text-center">
<h1>On form inputs focus, show div. hide div on blur for <span class="text-danger">multiple inputs</span> and hidden divs</h1>
<p class="lead"><i><a href="https://stackoverflow.com/questions/2426438/jquery-on-form-input-focus-show-div-hide-div-on-blur-with-a-caveat" target="_blank">Based from SO sigular example</a></i></p>
</div>
</div>
<div class="row">
<!--TEXT FIELDS INPUT ROW-->
<!--TEXT FIELDS INPUT ROW-->
<div id="select-data-inputs" class="controls form-row p-3 w-100">
<div class="col-4">
<input type="text" id="search-markets" class="input form-control" placeholder="Search Markets">
</div>
<div class="col-4 offset-1">
<input type="text" id="search-symbols" class="input form-control" placeholder="Search Symbols">
</div>
</div>
</div>
<div id="main-display">
<!--HIDDEN DIV FOR FIRST TEXT FIELD-->
<!--HIDDEN DIV FOR FIRST TEXT FIELD-->
<div id="select-markets" class="row select-filters market p-4">
<div class="select-heading col-12 pl-2">
<h6 class="small-sub-heading">Select markets</h6>
</div>
<div class="col-4 pt-2 select-filter-items">
<ul class="filter-list list-unstyled pl-2">
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-1" value="market-option-1">
<label class="form-check-label" for="market-option-1">Market Option 1</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-2" value="market-option-2">
<label class="form-check-label" for="market-option-2">Market Option 2</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-3" value="market-option-3">
<label class="form-check-label" for="market-option-3">Market-Option 3</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="market-option-4" value="market-option-4">
<label class="form-check-label" for="market-option-4">Market-Option 4</label>
</li>
</ul>
</div>
</div>
<!--HIDDEN DIV FOR SECOND TEXT FIELD-->
<!--HIDDEN DIV FOR SECOND TEXT FIELD-->
<div id="select-symbols" class="row select-filters symbol p-4">
<div class="select-heading col-4 offset-5 pl-2">
<h6 class="small-sub-heading">Select symbols</h6>
</div>
<div class="col-4 offset-5 pt-2 select-filter-items">
<ul class="filter-list list-unstyled pl-2">
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-1" value="symbol-option-1">
<label class="form-check-label" for="symbol-option-1">Symbol Option 1</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-2" value="symbol-option-2">
<label class="form-check-label" for="symbol-option-2">Symbol Option 2</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-3" value="symbol-option-3">
<label class="form-check-label" for="symbol-option-3">Symbol Option 3</label>
</li>
<li class="list-inline-item">
<input class="form-check-input" type="checkbox" id="symbol-option-4" value="symbol-option-4">
<label class="form-check-label" for="symbol-option-4">Symbol Option 4</label>
</li>
</ul>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.bundle.min.js" integrity="sha384-pjaaA8dDz/5BgdFUPX6M/9SUZv4d12SUPF0axWc+VRZkx5xU3daN+lYb49+Ax+Tl" crossorigin="anonymous"></script>
您需要在交易品种和市场(.select-filters
之间划分类别。
请注意,我将select-filters
market
用于市场,并将select-filters
symbol
用于符号。
要分离焦点,您需要使用诸如div.select-filters.market
的指定函数和div.select-filters.symbol
的符号的指定函数:
$('#search-markets').focus(function() {
$('div.select-filters.market').css('display', 'flex');
$(document).bind('focusin.select-filters.market click.select-filters.market',function(e) {
if ($(e.target).closest('.select-filters.market, #search-markets').length) return;
$(document).unbind('.select-filters.market');
$('div.select-filters.market').slideUp(300);
});
});
$('#search-symbols').focus(function() {
$('div.select-filters.symbol').css('display', 'flex');
$(document).bind('focusin.select-filters.symbol click.select-filters.symbol',function(e) {
if ($(e.target).closest('.select-filters.symbol, #search-symbols').length) return;
$(document).unbind('.select-filters.symbol');
$('div.select-filters.symbol').slideUp(300);
});
});
$('div.select-filters').hide();