jQuery e.stopPropagation() - how to use without br

2019-07-18 17:09发布

问题:

Short story: stopPropagation() prevents a dropdown menu from closing - which is good. But it also prevents the dropbox from opening next time around - which is bad.

Long story: I'm using Twitter-Bootstrap and I've put a search box inside the dropdown menu like so:

<div id="search_word_menu" style="position:absolute;right:157px;top:60px;"> 
        <ul class="nav nav-pills">
            <li class="dropdown" id="menu200">
                <a class="dropdown-toggle btn-inverse" data-toggle="dropdown" style="width:117px;position:relative;left:2px" href="#menu200">
                <i class="icon-th-list icon-white"></i>
                    Testing
                    <b class="caret"></b>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="#">Retweets</a></li>
                    <li><a href="#">Favourites</a></li>
                    <li class="divider"></li>
                    <li><a href="#">A list</a></li>
                    <li class="divider"></li>
                    <li><a href="#">A saved search</a></li>
                    <li><a href="#">A saved #hashtag</a></li>
                    <li class="divider"></li>
                    <li>
<!--   HERE -->     <input id="drop_search" type="text" class="search_box_in_menu" value="Search...">

                    </li>
                </ul>
            </li>
        </ul>

When I click inside the searchbox, the default behaviour is obviously to close the dropdown - but that makes it rather hard to write in a search term. So I've tried with the e.stopPropagation(), which does indeed prevent the dropdown from closing. Then, when I press enter in the searchbox, I'm closing the dropdown with a .toggle() - also seems to work fine.

The PROBLEM arises when I want to to it all again, because the e.stopPropagation() has now disabled the dropdown alltogether - ie. when I press the dropdown menu, it doesn't open anymore! This is because of stopPropagation(), no doubt - but how can I resolve this, so that I get the aforementioned functionality, but without breaking the rest altogether?

jQuery below:

$(document).ready(function() {
    console.log("document.ready - ");

                //clearing search box on click
    $(".search_box_in_menu").click(function(e) {
        e.stopPropagation(); // works for the specific task
        console.log(".search_box_in_menu - click.");
        if($(this).val() == 'Search...') {
            $(this).val('');
            console.log(".search_box_in_menu - value removed.");

        };
        //return false; //this is e.preventDefault() and e.stopPropagation()


    });

                    // when pressing enter key in search box
    $('.search_box_in_menu').keypress(function(e) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        if(keycode == '13') {
            console.log(".search_box_in_menu - enter-key pressed.");
            console.log($(this).val());
            $(this).closest('.dropdown-menu').toggle(); //works

        }
    });


    $('.dropdown').click(function() {
        console.log(".dropdown - click.");
        $(this).closest('.dropdown-toggle').toggle(); //does nothing
    });

Would greatly appreciate some help! I'm starting to suspect this might be a bootstrapped-only problem, or at least caused by their implementation - but it's beyond me atm.

回答1:

Found a solution: Using stopPropagation(), and closing box with manual trigger. Then, for some reason (is the stopPropagation() reset?), it works.

//dealing with the dropdown box
$(document).ready(function() {
    console.log("document.ready - ");

    //clearing search box on click and prevent the dropdown from closing
    $(".search_box_in_menu").click(function(e) {
        e.stopPropagation();
        console.log(".search_box_in_menu - click.");
        if($(this).val() == 'Search...') {
            $(this).val('');
            console.log(".search_box_in_menu - value removed.");
        };
        //return false;         

    });

    // when pressing enter key in search box
    $('.search_box_in_menu').keypress(function(e) {
        var keycode = (e.keyCode ? e.keyCode : e.which);
        if(keycode == '13') {
            console.log(".search_box_in_menu - enter-key pressed.");
            console.log($(this).val());
            $(this).closest('.dropdown').trigger('click');
        }
    });

    //resetting a dirty search box
    $('.dropdown').click(function() {
        if ($(this).closest('.search_box_in_menu').val() == 'Search...') {
            console.log(".search_box_in_menu - clean searchbox.");
        }
        console.log(".dropdown - click.");
    });     
});