Answer the question
In order to leave comments, you need to log in
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(); }",
],
]) ?>
"select2:selecting" => "function() { return searchDataAppeal(); }",
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);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question