Bootstrap-select - how to fire event on change

后端 未结 6 1386
隐瞒了意图╮
隐瞒了意图╮ 2020-12-08 09:30

I\'m using Bootstrap 3.0.2 and the Bootstrap-select plugin.

Here\'s my select list:



        
相关标签:
6条回答
  • 2020-12-08 09:49
    $("#Id").change(function(){
        var selected = $('#Id option:selected').val();
        alert(selected);           
    });
    

    I think this is what you need.

    0 讨论(0)
  • 2020-12-08 09:51

    This is what I did.

    $('.selectpicker').on('changed.bs.select', function (e, clickedIndex, newValue, oldValue) {
        var selected = $(e.currentTarget).val();
    });
    
    0 讨论(0)
  • 2020-12-08 09:55

    Simplest solution would be -

    $('.selectpicker').trigger('change');

    0 讨论(0)
  • 2020-12-08 10:05

    My implementation

    import $ from 'jquery';
    
    $(document).ready(() => {
      $('#whatDescribesYouSelectInput').on('change', (e) => {
        if (e.target.value === 'Other') {
          $('#whatDescribesYouOtherInput').attr('type', 'text');
          $('#specifyLabel').show();
        } else {
          $('#whatDescribesYouOtherInput').attr('type', 'hidden');
          $('#specifyLabel').hide();
        }
      });
    });
    
    0 讨论(0)
  • 2020-12-08 10:07

    When Bootstrap Select initializes, it'll build a set of custom divs that run alongside the original <select> element and will typically synchronize state between the two input mechanisms.

    Which is to say that one way to handle events on bootstrap select is to listen for events on the original select that it modifies, regardless of who updated it.

    Solution 1 - Native Events

    Just listen for a change event and get the selected value using javascript or jQuery like this:

    $('select').on('change', function(e){
      console.log(this.value,
                  this.options[this.selectedIndex].value,
                  $(this).find("option:selected").val(),);
    });
    

    *NOTE: As with any script reliant on the DOM, make sure you wait for the DOM ready event before executing

    Demo in Stack Snippets:

    $(function() {
    
      $('select').on('change', function(e){
        console.log(this.value,
                    this.options[this.selectedIndex].value,
                    $(this).find("option:selected").val(),);
      });
      
    });
    <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
    <link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.css" rel="stylesheet"/>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.js"></script>
    
    <select class="selectpicker">
      <option val="Must"> Mustard </option>
      <option val="Cat" > Ketchup </option>
      <option val="Rel" > Relish  </option>
    </select>

    Solution 2 - Bootstrap Select Custom Events

    As this answer alludes, Bootstrap Select has their own set of custom events, including changed.bs.select which:

    fires after the select's value has been changed. It passes through event, clickedIndex, newValue, oldValue.

    And you can use that like this:

    $("select").on("changed.bs.select", 
          function(e, clickedIndex, newValue, oldValue) {
        console.log(this.value, clickedIndex, newValue, oldValue)
    });
    

    Demo in Stack Snippets:

    $(function() {
    
      $("select").on("changed.bs.select", 
            function(e, clickedIndex, newValue, oldValue) {
          console.log(this.value, clickedIndex, newValue, oldValue)
      });
    
    });
    <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/css/bootstrap.css" rel="stylesheet"/>
    <link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/css/bootstrap-select.css" rel="stylesheet"/>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.2/js/bootstrap.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.12.4/js/bootstrap-select.js"></script>
    
    <select class="selectpicker">
      <option val="Must"> Mustard </option>
      <option val="Cat" > Ketchup </option>
      <option val="Rel" > Relish  </option>
    </select>

    0 讨论(0)
  • 2020-12-08 10:11

    Easiest implementation.

    <script>
        $( ".selectpicker" ).change(function() {
            alert( "Handler for .change() called." );
        });
    </script>
    
    0 讨论(0)
提交回复
热议问题