【问题标题】:On form inputs focus, show div. hide div on blur for multiple inputs and hidden divs在表单输入焦点上,显示 div。为多个输入和隐藏的 div 隐藏 div
【发布时间】:2019-06-17 16:36:21
【问题描述】:

我有 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>

【问题讨论】:

    标签: jquery input focus hidden blur


    【解决方案1】:

    我想你想要的是这样的:

    //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)。 请注意,我使用 add select-filtersmarket 表示市场,select-filterssymbol 表示符号。

    要分离焦点,您需要使用指定的函数,例如 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();
    

    【讨论】:

    • 是的,谢谢,但看起来有很多重复的代码。可以重构或改进吗?
    • @Kerry7777 我认为我们可以删除 div.select-filters 的 select-filters2。并添加一个自定义类,如marketsymbol。但是对于javascript函数,我认为最好有一个指定的。
    • @Kerry7777 有关代码重构和改进的更多信息,最好询问here
    • 我根据 Stack Exchange 代码审查中的建议提出了重构/改进脚本的问题:codereview.stackexchange.com/questions/212253/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多