如何使用jquery选择select2 selectbox的选项?

时间:2016-11-24 09:03:08

标签: jquery select2

我尝试用jquery选择我的选择框的bird。但无论我做什么,它都会cat

$(function () {
  $(".select2").select2();
   $(".select2").val('bird');
      $('.select2').find('option[value="bird"]').prop('selected', true); 
});
<link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet"/>
<link href="https://select2.github.io/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://select2.github.io/vendor/js/bootstrap.min.js"></script>
<script src="https://select2.github.io/dist/js/select2.full.js"></script>

<div class="form-group">
  <select class=" form-control select2"  style="width: 100%;">
    <option value="cat">Cat</option>
    <option value="dog">Dog</option>
    <option value="bird">Bird</option>
  </select>
</div>

3 个答案:

答案 0 :(得分:2)

您需要在设置值后触发change事件,如下所示:

$(function () {
  $(".select2").select2();
   $(".select2").val('bird').trigger("change");
      
});
<link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet"/>
<link href="https://select2.github.io/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://select2.github.io/vendor/js/bootstrap.min.js"></script>
<script src="https://select2.github.io/dist/js/select2.full.js"></script>

<div class="form-group">
  <select class=" form-control select2"  style="width: 100%;">
    <option value="cat">Cat</option>
    <option value="dog">Dog</option>
    <option value="bird">Bird</option>
  </select>
</div>

答案 1 :(得分:1)

$(function () {
  $(".select2").select2();
   $(".select2").val('bird');
      $('.select2').find('option[value="bird"]').prop('selected', true).change(); 
});
<link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet"/>
<link href="https://select2.github.io/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://select2.github.io/vendor/js/bootstrap.min.js"></script>
<script src="https://select2.github.io/dist/js/select2.full.js"></script>

<div class="form-group">
  <select class=" form-control select2"  style="width: 100%;">
    <option value="cat">Cat</option>
    <option value="dog">Dog</option>
    <option value="bird">Bird</option>
  </select>
</div>

答案 2 :(得分:0)

如果添加到选项,

Select2会尊重您的初始selected属性。我不确定这是否适用于您的情况,但它可能是最具语义性的解决方案。

$(function () {
  $(".select2").select2();
});
<link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet"/>
<link href="https://select2.github.io/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://select2.github.io/vendor/js/bootstrap.min.js"></script>
<script src="https://select2.github.io/dist/js/select2.full.js"></script>

<div class="form-group">
  <select class=" form-control select2"  style="width: 100%;">
    <option value="cat">Cat</option>
    <option value="dog">Dog</option>
    <option selected value="bird">Bird</option>
  </select>
</div>