Get selected item value from Bootstrap DropDown with specific ID

前端 未结 7 705
鱼传尺愫
鱼传尺愫 2020-12-24 06:10

I am \"creating\" my own \"ComboBox\" using Bootstrap 3 and JavaScript. Here is the JSFiddle for what I have so far:

相关标签:
7条回答
  • 2020-12-24 06:50

    You might want to modify your jQuery code a bit to '#demolist li a' so it specifically selects the text that is in the link rather than the text that is in the li element. That would allow you to have a sub-menu without causing issues. Also since your are specifically selecting the a tag you can access it with $(this).text();.

    $('#datebox li a').on('click', function(){
        //$('#datebox').val($(this).text());
        alert($(this).text());
    });
    
    0 讨论(0)
  • 2020-12-24 06:52
    $('#demolist li').on('click', function(){
        $('#datebox').val($(this).text());
    });
    

    http://jsfiddle.net/kcpma/18/

    0 讨论(0)
  • 2020-12-24 06:53

    Did you just try

    $('#datebox li a').on('click', function(){
        //$('#datebox').val($(this).text());
        alert($(this).text());
    });
    

    It works for me :)

    0 讨论(0)
  • 2020-12-24 07:04

    Design code using Bootstrap

    <div class="dropdown-menu" id="demolist">
      <a class="dropdown-item" h ref="#">Cricket</a>
      <a class="dropdown-item" href="#">UFC</a>
      <a class="dropdown-item" href="#">Football</a>
      <a class="dropdown-item" href="#">Basketball</a>
    </div>

    • jquery Code

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
      $(document).ready(function () {
        $('#demolist a').on('click', function () {
          var txt= ($(this).text());
          alert("Your Favourite Sports is "+txt);
        });
      });
    </script>

    0 讨论(0)
  • 2020-12-24 07:06

    The selector would be #demolist.dropdown-menu li a note no space between id and class. However i would suggest a more generic approach:

    <div class="input-group">                                            
        <input type="TextBox" Class="form-control datebox"></input>
        <div class="input-group-btn">
        <button type="button" class="btn dropdown-toggle" data-toggle="dropdown">
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu">
            <li><a href="#">A</a></li>
            <li><a href="#">B</a></li>
            <li><a href="#">C</a></li>
        </ul>
    </div>
    
    
    $(document).on('click', '.dropdown-menu li a', function() {
        $(this).parent().parent().parent().find('.datebox').val($(this).html());
    }); 
    

    by using a class rather than id, and using parent().find(), you can have as many of these on a page as you like, with no duplicated js

    0 讨论(0)
  • 2020-12-24 07:12

    Try this code

    <input type="TextBox" ID="yearBox" border="0" disabled>
    
    $('#yearSelected li').on('click', function(){
                    $('#yearBox').val($(this).text());
                });
    
    
    
    <a href="#" class="dropdown-toggle" data-toggle="dropdown"> <i class="fas fa-calendar-alt"></i> <span>Academic Years</span> <i class="fas fa-chevron-down"></i> </a>
              <ul class="dropdown-menu">
                <li>
                  <ul class="menu" id="yearSelected">
                    <li><a href="#">2014-2015</a></li>
                    <li><a href="#">2015-2016</a></li>
                    <li><a href="#">2016-2017</a></li>
                    <li><a href="#">2017-2018</a></li>
    
                  </ul>
                </li>
              </ul>
    

    its work for me

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