简体   繁体   中英

Drop-down get Selected item Text

I want to change my button text when my dropdown item is clicked. I am using this code block but I can't do it:

<div class="dropdown">
    <button class="btn btn-danger dropdown-toggle" type="button" data-toggle="dropdown">Alan Ekle
    <span class="caret"></span></button>
    <ul id="Secenekler" class="dropdown-menu">
      <li><a href="#">Isim</a></li>
      <li><a href="#">SoyIsim</a></li>
      <li><a href="#">Adress</a></li>
      <li><a href="#">Numara</a></li>
      <li><a href="#">Yaş</a></li>
      <li><a href="#">Tanıdık 1</a></li>
    </ul>
  </div>

And my script. I don't get the alert() message.

$(".dropdown-menu li a").click(function(){           
    alert("tiklandi");

    $(this).parents(".dropdown").find('.btn').html( $(this).text()  );

    console.log($(this).text());
    console.log($(this).text($(this).parents(".dropdown").find('.btn')));
});

try this. TESTED : https://jsfiddle.net/wy52thbf/

Good Luck

      $(document).ready(function() {
        $(".dropdown-menu li a").click(function() {
          var getText = $(this).text();
          /*console.log(getText);
          console.log($(this).closest('.dropdown').find('.btn'));*/
          $(this).closest('.dropdown').find('.btn').text(getText);
        });
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM