使用 JS click() 打开 Select2 下拉列表
Open Select2 dropdown with JS click()
我已经尝试在容器内的每个元素(包括容器本身)上使用 .click() 函数模拟点击。下拉菜单不会打开。用鼠标点击可以正常工作。
<div class="select2-container select2-allowclear editor s-LookupEditor valid" id="s2id_AbCd_Q_CustomerEditDialog22_CstId">
<a href="javascript:void(0)" class="select2-choice" tabindex="-1">
<span class="select2-chosen" id="select2-chosen-2">Peter Smith</span>
<abbr class="select2-search-choice-close"></abbr>
<span class="select2-arrow" role="presentation">
<b role="presentation"></b>
</span>
</a>
<label for="s2id_autogen2" class="select2-offscreen">Customer</label>
<input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-2" id="s2id_autogen2">
要以编程方式打开 Select2 实例,请对其调用 open
方法:
$('select').select2(); // initialise
$('select').select2('open'); //open
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script>
<select>
<option>Please select</option>
<option>Foo</option>
<option>Bar</option>
</select>
我已经尝试在容器内的每个元素(包括容器本身)上使用 .click() 函数模拟点击。下拉菜单不会打开。用鼠标点击可以正常工作。
<div class="select2-container select2-allowclear editor s-LookupEditor valid" id="s2id_AbCd_Q_CustomerEditDialog22_CstId">
<a href="javascript:void(0)" class="select2-choice" tabindex="-1">
<span class="select2-chosen" id="select2-chosen-2">Peter Smith</span>
<abbr class="select2-search-choice-close"></abbr>
<span class="select2-arrow" role="presentation">
<b role="presentation"></b>
</span>
</a>
<label for="s2id_autogen2" class="select2-offscreen">Customer</label>
<input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-2" id="s2id_autogen2">
要以编程方式打开 Select2 实例,请对其调用 open
方法:
$('select').select2(); // initialise
$('select').select2('open'); //open
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script>
<select>
<option>Please select</option>
<option>Foo</option>
<option>Bar</option>
</select>