Update hidden field using Autocomplete in Yii2

前端 未结 1 1060
南旧
南旧 2021-01-03 06:52

I am working on an autocomplete widget. So far this is my code.

My view:

    select([\'name as value\',         


        
相关标签:
1条回答
  • 2021-01-03 07:22

    You can use these properties:

    • labels - shown in dropdown,
    • value - jumps to input field after selection,
    • id - additional paramter for hiden field usage.

    For example:

    <input type="hidden" id="user_company" name="user_company" value="qwe">
    <?php
    
      use yii\web\JsExpression;
    
      echo AutoComplete::widget([
        'name' => 'company',
        'id' => 'ffffd',
        'clientOptions' => [
          'source' => [
              ['label'=>'color1', 'value'=>'key1', 'id'=>'c_id1'],
              ['label'=>'color2', 'value'=>'key2', 'id'=>'c_id2']
          ],
          'autoFill'=>true,
          'minLength'=>'0',
          'select' => new JsExpression("function( event, ui ) {
            console.log(ui);
            $('#user_company').val(ui.item.id);
          }")
    
    
        ],
    
      ]);?>
    
    0 讨论(0)
提交回复
热议问题