首页 > 解决方案 > 如何通过 Yii2 kartik typeahead 远程 URL 从文本字段传递动态值

问题描述

我像这样在我的网站中使用 yii2 typeahead

<?=$form->field($model, 'building',['inputOptions' => [
                  'autocomplete' => 'off']])->widget(Typeahead::className(),[
                  'name' => 'building_profile',
                  'options' => ['placeholder' => 'Type your building','class'=>'autocomplete','id'=>'building_profile','autoFocus'=>true],
                  'pluginOptions' => ['highlight'=>true,'autoselect' => true],
                  'dataset' => [
                        [
                          'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('value')",
                          'display' => 'value',
                          'remote' => [
                              'url' => Url::to(['sale-residential/all-buildings']) . '?q=%QUERY&country_id='.$country_id,
                              'wildcard' => '%QUERY'
                        ],
                          'limit'=>10
                        ]
                  ],
                  'pluginEvents'=>[
                    "typeahead:select" => "function() { getBuildingDetails(this.value); }",
                  ]
        ])->label(false);?>

这是我的代码。请看一下。url中有一个country_id。现在我正在向它传递一个静态值。它现在正在工作。但问题是我想将一个动态值从一个文本字段传递到该网址类似于

'url' => Url::to(['sale-residential/all-buildings']) . '?q=%QUERY&country_id='.{get the value from a text field with id my_country}

我该怎么做。提前谢谢。我的文本字段在同一个视图文件中

<?=$form->field($model,'country_id')->textInput(['id'=>'my_country')?>

实际上我的需要是,当我提前输入时,我想通过远程 url 传递用户输入的国家/地区 ID。这就是我想要的

标签: phpyii2typeaheadkartik-v

解决方案


推荐阅读