最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery prevent default functionality in dropdown menu - Stack Overflow

matteradmin6PV0评论

I have created a smart search ponent inside a dropdown menu. User should be allowed to search within the dropdown depending upon the value entered in the input=text provided in menu.

But when i am clicking on the input the menu gets closed. I tried using preventDefault as well but still it is not working.

HTML:

<input type="text" readonly="readonly" value="">
<button class="btn i dropdown-toggle" data-toggle="dropdown"> <i class="caret-down"></i>
</button>
<div class="dropdown-menu smart">
    <input type="text" placeholder="Search">
    <ul class="search-options">
        <li><a href="#">Something</span></a>
        </li>
    </ul>
</div>

JS:

// jQuery to prevent default close 
$('body').on('click', '.dropdown-menu.smart input', function (event) {
    event.preventDefault();
});

I have created a smart search ponent inside a dropdown menu. User should be allowed to search within the dropdown depending upon the value entered in the input=text provided in menu.

But when i am clicking on the input the menu gets closed. I tried using preventDefault as well but still it is not working.

HTML:

<input type="text" readonly="readonly" value="">
<button class="btn i dropdown-toggle" data-toggle="dropdown"> <i class="caret-down"></i>
</button>
<div class="dropdown-menu smart">
    <input type="text" placeholder="Search">
    <ul class="search-options">
        <li><a href="#">Something</span></a>
        </li>
    </ul>
</div>

JS:

// jQuery to prevent default close 
$('body').on('click', '.dropdown-menu.smart input', function (event) {
    event.preventDefault();
});
Share Improve this question edited Nov 22, 2013 at 12:15 palaѕн 74k17 gold badges122 silver badges139 bronze badges asked Nov 22, 2013 at 12:13 well_alrightwell_alright 1131 gold badge2 silver badges5 bronze badges 1
  • Your code seems to be correct and should be working. But this is inplete - plz. provide also css and dropdown code so I'll be able to check this out. Jsfiddle would be most apreciated. – Hexodus Commented Nov 22, 2013 at 12:20
Add a ment  | 

1 Answer 1

Reset to default 5

Write the following code and it should keep the dropdown open

$('.dropdown-menu').click(function(e) {
        e.stopPropagation();
});
Post a comment

comment list (0)

  1. No comments so far