How do I get selected value from a bootstrap select drop down

前端 未结 3 1962
盖世英雄少女心
盖世英雄少女心 2021-01-01 14:59

I\'m trying to get the selected text, not value, from my bootstrap drop down, but my .text() statement is returning a string that contains all the values with a \'\\n\' in b

相关标签:
3条回答
  • 2021-01-01 15:10

    Fiddle for you

     $(document).ready(function () {
         $('.chzn-select').change(function () {
             alert( $('.chzn-select option:selected').text());
         });
     });
    
    <select id="second" class="chzn-select" style="width: 100px">
        <option value="1">one</option>
        <option value="2">two</option>
    </select>
    

    This is based on the css3 psuedo-class :selected. It's very similar to :checked, I couldn't find docs for :selected

    0 讨论(0)
  • 2021-01-01 15:18

    In case anyone cares, I've got another solution. I just looked at the arguments from the docs. You can do something like this (Assuming you've set the value tag of the option element.:

     $('#type_dropdown')
                    .on('changed.bs.select',
                        function(e, clickedIndex, newValue, oldValue) {
                            alert(e.target.value);
                        });
            });
    

    See https://silviomoreto.github.io/bootstrap-select/options/

    0 讨论(0)
  • 2021-01-01 15:30

    You can get the selected value's text with $(this).find("option:selected").text().

    $('#SpaceAccommodation').change(function () {
        var selectedText = $(this).find("option:selected").text();
        
        $(".test").text(selectedText);
    });
    <script src="https://code.jquery.com/jquery-1.6.4.min.js"></script>
    <select class="form-control" id="SpaceAccommodation" name="YogaSpaceAccommodation">
        <option selected="selected" value="0">1-4</option>
        <option value="1">5-9</option>
        <option value="2">10-15</option>
        <option value="3">16-20</option>
        <option value="4">20+</option>
    </select>
    <div class="test"></div>

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