Show selected option in bootstrap dropdown list menu

后端 未结 5 1417
迷失自我
迷失自我 2020-12-28 15:56

I have dropdown list in my bootstrap menu.

  • 相关标签:
    5条回答
    • 2020-12-28 16:14

      Change your html like :

       <li class="dropdown">
          <button id="options" aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" >Chose option<span class="caret"></span></button>
              <ul class="dropdown-menu">
                  <li><a href="#" >Option 1</a></li>
                  <li><a href="#">Option 2</a></li>
                  <li><a href="#" >Option 3</a></li>
                  <li><a href="#" >Option 4</a></li>
              </ul>
      </li>
      

      and on javascript side do like this way :

       <script>
      
              $(document).ready(function(){
                  $(".dropdown-menu li a").click(function(){
                  $("#options").text($(this).text());
                  });
              });
          </script>
      
      0 讨论(0)
    • 2020-12-28 16:16

      I had the same problem and here is the solution I found.

      <div class="btn-group">
          <button class="btn btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" id="options"> <span id="opt">Chose option</span> <span class="caret"></span>
          </button>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" id="1" href="#" >Option 1</a></li>
              <li><a class="dropdown-item" id="2" href="#">Option 2</a></li>
              <li><a class="dropdown-item" id="3" href="#" >Option 3</a></li>
              <li><a class="dropdown-item" id="4" href="#" >Option 4</a></li>
            </ul>
      </div>
      
      <script type="text/javascript">
      $(function(){
        $("#1").click(function () {
        $("#opt").text($(this).text());
        });
        $("#2").click(function () {
        $("#opt").text($(this).text());
        });
        $("#3").click(function () {
        $("#opt").text($(this).text());
        });
        $("#4").click(function () {
        $("#opt").text($(this).text());
        });
      });
      </script>
      
      0 讨论(0)
    • 2020-12-28 16:22

      This should work for you:

      <div class="dropdown">
          <a aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">
            <span id="selected">Chose option</span><span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="#">Option 1</a></li>
          <li><a href="#">Option 2</a></li>
          <li><a href="#">Option 3</a></li>
          <li><a href="#">Option 4</a></li>
        </ul>
      </div>
      

      And the script:

        $('.dropdown-menu a').click(function(){
          $('#selected').text($(this).text());
        });
      

      You need to wrap the text 'Choose option' inside <span> to modify its value on selection.

      Here is the working code: http://liveweave.com/U4BpiH

      0 讨论(0)
    • 2020-12-28 16:30

      You can go through this simple example.

      <div class="btn-group">
          <button class="btn">Please Select From List</button>
          <button class="btn dropdown-toggle" data-toggle="dropdown">
          <span class="caret"></span>
         </button>
       <ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu">
         <li><a tabindex="-1" href="#">Item I</a></li>
         <li><a tabindex="-1" href="#">Item II</a></li>
         <li><a tabindex="-1" href="#">Item III</a></li>
         <li class="divider"></li>
         <li><a tabindex="-1" href="#">Other</a></li>
       </ul>
      </div>
      

      Add the below script :

      $(function(){
      
          $(".dropdown-menu li a").click(function(){
      
            $(".btn:first-child").text($(this).text());
            $(".btn:first-child").val($(this).text());
      
         });
      
      });
      

      Also you can try this sample in http://jsbin.com/owuyix/4/edit

      0 讨论(0)
    • 2020-12-28 16:32

      I found a solution using "onclick"

      onclick="$('#your-main-button-name').html('The text you want to show');"
      

      Hope it helps! :)

      0 讨论(0)
    提交回复
    热议问题