2016-11-24 300 views
1

我嘗試用jquery選擇我的selectbox的bird。但它一直是cat,無論我做什麼:如何使用jquery選擇select2選擇框的選項?

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

試$( '選擇2 ')。找到(' 選項[值= 「鳥」] ')。支撐(' 選擇',真).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

如果加入一個選項,選擇二尊重您最初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>

相關問題