A
A
Alexander Laikov2020-12-15 22:24:13
Yii
Alexander Laikov, 2020-12-15 22:24:13

How to get value value of Select2 kartik widget?

Good afternoon.
View has a Select2 widget

<?= $form->field($model, 'subscriber', [
          'template' => '
            <div class="form-group">
              <label for="field-1" class="col-sm-3 control-label">Абонент</label>
                <div class="col-sm-5">
                  {input}
                </div
            </div>
          '
        ])->widget(Select2::classname(), [
            'options' => [
            	'placeholder' => 'Введите номер ...'
            ],
            'pluginOptions' => [
                'allowClear' => true,
                'minimumInputLength' => 3,
                'language' => [
                    'errorLoading' => new JsExpression("function () { return 'Поиск результатов...'; }"),
                ],
                'ajax' => [
                    'url' => $url,
                    'dataType' => 'json',
                    'data' => new JsExpression('function(params) { return {q:params.term}; }')
                ],
                'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                'templateResult' => new JsExpression('function(subscriber) { return subscriber.text; }'),
                'templateSelection' => new JsExpression('function (subscriber) { return subscriber.text; }'),

            ],
            'pluginEvents' => [
            	"select2:selecting" => "function() { return searchDataAppeal(); }",
            ],
        ]) ?>


after the user has selected the desired value, I call the function through:
"select2:selecting" => "function() { return searchDataAppeal(); }",


The JS function itself looks like this:

function searchDataAppeal() {

  var subscriber = $('#appealform-subscriber').select2("data")[0]. id; // получаем текст пусто значение
  var subscriber = $('#appealform-subscriber').select2("data")[0].text; // получаем текст "Поиск результатов..."
  var subscriber = $('#appealform-subscriber').select2("data")[1]. id; // получаем: Uncaught TypeError: Cannot read property 'id' of undefined
  //alert(subscriber);
}


What is the problem? Or how to get the correctly selected option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
magalalka, 2022-02-20
@magalalka

Hello. Do you remember how you solved the problem?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question