egorfadeev
egorfadeev

Reputation: 57

AutoComplete on select

After I type something in input field, and then select the suggested value, js gets the value I typed, but I need the value I select.

For example: I have a db with car names. I need to find some "Mercedes". I type letter 'M', it shows me 'Mercedes, Mazda, etc.'. After I select 'Mercedes', the js alerts me just 'M', while I need a full value.

What should be edited?

View

<?= $form->field($model, 'name')
->widget(\yii\jui\AutoComplete::classname(), [
                    'clientOptions' => [
                        'source' => array_values($suggest),
                        'select' => new JsExpression("function() {
                var name = $(this).val();
                alert(name);
             }")
                    ], 'options' => ['class' => 'searchinput', 'id' => 'searchByName'],
                ])->label(false) ?>

Upvotes: 0

Views: 65

Answers (1)

Patrick
Patrick

Reputation: 1338

The "select" event fires before the value of the input is actually changed:

http://api.jqueryui.com/autocomplete/#event-select

Try this instead:

'select' => new JsExpression("function(event, ui) {
    alert(ui.item.value);
}")

Upvotes: 1

Related Questions