我動態添加select2類到當前點擊元素,但搜索框在第二次點擊後出現。它應該首先點擊元素。動態添加select2類到當前點擊元素
例:
$(document).on('click', 'select', function() {
$(this).select2();
});
感謝
我動態添加select2類到當前點擊元素,但搜索框在第二次點擊後出現。它應該首先點擊元素。動態添加select2類到當前點擊元素
例:
$(document).on('click', 'select', function() {
$(this).select2();
});
感謝
後,選擇2已經初始化那麼我們就可以用命令再次調用它.select2('open')
希望這是你在找什麼。
$(document).on('click', 'select', function() {
var select2_open;
$(this).select2().select2('open');
});
<link href="https://select2.github.io/dist/css/select2.min.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/dist/js/select2.full.js"></script>
<select class="select">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>
它的工作, 謝謝 –
@VijayKumarYadav非常歡迎 –
我認爲你需要再讀一遍這個問題。他這樣做是爲了動態創建元素。 –
哦,對不起,我的壞。謝啦。 –
$('.select').select2();
$(document).on('click', 'select', function() {
$(this).select2();
});
<link href="https://select2.github.io/dist/css/select2.min.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/dist/js/select2.full.js"></script>
<select class="select">
<option value="AL">Alabama</option>
<option value="WY">Wyoming</option>
</select>
您能否向我們提供您的問題的工作片段? –
它工作https://jsfiddle.net/br3t/z5ogefz8/ – br3t
嘗試用'$(this).select2()。select2('open');'替換行$(this).select2();'但是爲什麼你需要這樣的行爲?從邏輯上講,所有的選擇框都應該在頁面加載時轉換爲'select2'以具有良好的用戶體驗。 https://jsfiddle.net/z5ogefz8/2/ – vijayP