0

我有一个这样创建的字段:

ChoiceField::new('type')->onlyWhenCreating()->setChoices($this->menuService->getAsChoices())->addCssClass('menu-type'),

这会产生:

<div class="field-select menu-type form-group">
    <label class="form-control-label" for="Menu_type">Tips</label>
    <div class="form-widget">
        <select id="Menu_type" name="Menu[type]" data-widget="select2" data-ea-escape-markup="false"
                class="form-control select2-hidden-accessible" data-select2-id="Menu_type" tabindex="-1"
                aria-hidden="true">
            <option value="" data-select2-id="13"></option>
            <option value="News" data-select2-id="5">Bloga ieraksts</option>
            <option value="Initiative" data-select2-id="14">Iniciatīva</option>
            <option value="Content" data-select2-id="15">Saturs</option>
            <option value="Category" data-select2-id="16">Kategorija</option>
            <option value="UserGroup" data-select2-id="17">Lietotāju grupa</option>
        </select>
        <span class="select2 select2-container select2-container--bootstrap select2-container--below select2-container--focus"
              dir="ltr" data-select2-id="4" style="width: 350px;">
            <span class="selection">
                <span class="select2-selection select2-selection--single" role="combobox" aria-haspopup="true"
                      aria-expanded="false" tabindex="0" aria-disabled="false"
                      aria-labelledby="select2-Menu_type-container">
                    <span class="select2-selection__rendered"
                          id="select2-Menu_type-container"
                          role="textbox" aria-readonly="true"
                          title="Iniciatīva">
                        <span class="select2-selection__clear" title="Remove all items" data-select2-id="18">×</span>
                        Iniciatīva
                    </span>
                    <span class="select2-selection__arrow" role="presentation">
                            <b role="presentation"></b>
                    </span>
                </span>
            </span>
            <span class="dropdown-wrapper" aria-hidden="true">
                
            </span>
        </span>
    </div>
</div>

但是当试图访问所谓的触发事件时

global.$ = global.jQuery = require('jquery');

$(document).ready(function ($) {
    $('.form-widget').on('select2:select', '#Menu_type', function (e) {
        console.log(e);
    });
    $('#Menu_type').on('select2:select', function (e) {
        console.log(e);
    });
});

我想念什么?

- - 更新 - -

经过一些进一步的调查,看起来 select2 创建得太晚了,无法使用它。我修改了我的代码:

import $ from 'jquery';

$(document).ready(function () {
    $(document).on('select2:select', '#Menu_type', function (e) {
        let entitySelect = $('#Menu_entityLinkId');
        entitySelect.find('option').remove();
        $.ajax(
            '/ajax/getmenuentries',
            {
                type: "POST",
                data: {'type': e.params.data.id},
                dataType: 'json',
                complete: function (result) {
                    if (result.responseJSON.errors.length === 0) {
                        $('.menu-entity').css('display', 'flex');
                        $.each(result.responseJSON.allowed, function (index, value) {
                            entitySelect.append(new Option(value, index));
                        });
                        entitySelect.trigger('change');
                    } else {
                        console.log(result.responseJSON.errors);
                    }
                }
            }
        );
    });
});

但它仍然不起作用。

4

0 回答 0