2016-11-24 7 views
1

私はjqueryで私のselectboxのbirdを選択しようとしています。しかし、それは関係なく、私は何をすべきか、catならないように保持します:jQueryでselect2 selectboxのオプションを選択するにはどうすればよいですか?

$(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>

+2

トライ$( 'SELECT2 ')。を見つける(' オプション[値= "鳥"] ')。(、' 選択された' 真)小道具.change() –

答えて

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

$(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>
オプションに追加された場合

0

Selectセレクトはあなたの最初の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>

関連する問題