最新消息: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 - Handle Bootstrap dropdown event in jQuery - Stack Overflow

matteradmin5PV0评论

I have markup for twitter bootstrap dropdown like below

<div class="dropdown" style="width: 300px !important;">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
Source
</button>
<div class="dropdown-menu" id="SelectMe">
   <a class="dropdown-item" href="#">CLE</a>
   <a class="dropdown-item" href="#">SQL</a>
   <a class="dropdown-item" href="#">FFILE</a>
   <a class="dropdown-item" href="#">SampleAPI</a>
</div>
</div>

Now, added click event in dropdown using jquery as below

 $('.dropdown-menu a').click(function () {           
    alert("Hi")
  });

My click event is working but, I am not able to update the selected value in dropdown. How to do it?

I have markup for twitter bootstrap dropdown like below

<div class="dropdown" style="width: 300px !important;">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
Source
</button>
<div class="dropdown-menu" id="SelectMe">
   <a class="dropdown-item" href="#">CLE</a>
   <a class="dropdown-item" href="#">SQL</a>
   <a class="dropdown-item" href="#">FFILE</a>
   <a class="dropdown-item" href="#">SampleAPI</a>
</div>
</div>

Now, added click event in dropdown using jquery as below

 $('.dropdown-menu a').click(function () {           
    alert("Hi")
  });

My click event is working but, I am not able to update the selected value in dropdown. How to do it?

Share Improve this question edited Nov 23, 2018 at 10:38 IndexOutOfDevelopersException 1,3547 gold badges15 silver badges28 bronze badges asked Nov 23, 2018 at 8:26 RAMRAM 754 silver badges11 bronze badges 3
  • do you want to set the selected text as button text? – Kiranramchandran Commented Nov 23, 2018 at 8:38
  • @Kiranramchandran yes – RAM Commented Nov 23, 2018 at 8:40
  • check the answer – Kiranramchandran Commented Nov 23, 2018 at 8:49
Add a ment  | 

2 Answers 2

Reset to default 1

$('.dropdown-menu a').click(function () {           
    $('button').text($(this).text());
  });
<script src="https://ajax.googleapis./ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn./bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />


<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn./bootstrap/4.1.3/js/bootstrap.bundle.min.js"></script>
<script src="https://stackpath.bootstrapcdn./bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn./bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="dropdown" style="width: 300px !important;">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
Source
</button>
<div class="dropdown-menu" id="SelectMe">
   <a class="dropdown-item" href="#">CLE</a>
   <a class="dropdown-item" href="#">SQL</a>
   <a class="dropdown-item" href="#">FFILE</a>
   <a class="dropdown-item" href="#">SampleAPI</a>
</div>
</div>

use $('button').text($(this).text()); to set value to button.

try this

$('.dropdown-menu a').click(function () {           
    $('button[data-toggle="dropdown"]').text($(this).text());
});

DEMO HERE

Post a comment

comment list (0)

  1. No comments so far