如何将点击事件绑定到已经呈现的动态添加的select2下拉列表

时间:2018-08-29 23:40:32

标签: javascript jquery-select2

我试图将click事件添加到已经渲染的select2下拉菜单中。 select2将在加载时动态添加到页面中。这可能吗?我尝试将其添加到div类和许多其他类中,但是没有响应。

<div class="select2-drop select2-display-none select2-drop-auto-width select2-drop-active" style="left: 394.375px; top: 1289.56px; bottom: auto; display: block; width: auto;" id="select2-drop">
    <div class="select2-search select2-search-hidden select2-offscreen">
        <label for="s2id_autogen143_search" class="select2-offscreen"> </label>
        <input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="select2-results-143" id="s2id_autogen143_search" placeholder="" aria-activedescendant="select2-result-label-191"> </div>
    <ul class="select2-results" role="listbox" id="select2-results-143">
        <li class="select2-results-dept-0 select2-result select2-result-selectable select2-highlighted" role="presentation">
            <div class="select2-result-label" id="select2-result-label-191" role="option"> <span class="select2-match"> </span>the same time:</div>
        </li>
        <li class="select2-results-dept-0 select2-result select2-result-selectable" role="presentation">
            <div class="select2-result-label" id="select2-result-label-192" role="option"><span class="select2-match"></span> a different time: </div>
        </li>
    </ul>
</div>

我尝试过此版本:

$('body').on('click', '#select2-drop', function () {
  alert('wow');
});

或者这个:

$(document).on('click', '#select2-drop', function () {
  alert('wow');
});

我当前正在使用此

window.addEventListener('load', function () {

0 个答案:

没有答案