如何获取yii2 select2小部件的选定选项的值

sne*_*new 2 php jquery-select2 yii2 yii2-basic-app

我试图在同一页面中获取所选选项的值。我正在使用 select2 小部件。

<?php
echo '<label class="control-label">Group</label>';
$data = ['1' => 'option1', '2' => 'option2'];
echo Select2::widget([
    'name'=> 'group',
    'data' => $data,
    'options' => ['placeholder' => 'Select a option...', 'id' => 'sem'],
    'pluginOptions' => [
        'allowClear' => true
    ],
]);
?>
<input type="text" id='items' value="<?=$selectedvalue?> />'
Run Code Online (Sandbox Code Playgroud)

在这里,我想在输入框中获取该页面中的选定值。

<input type="text" id='items' value="<?=$selectedvalue?> />
Run Code Online (Sandbox Code Playgroud)

我正在使用 Yii2 我是新手。谁能帮我?

gmc*_*gmc 5

您可以使用 jQuery 获取选定的值:

$("#id_of_your_select2_widget").on("change", function (e) { 
    var id = $("#id_of_your_select2_widget").select2("data")[0].id;

   // Now you can work with the selected value, i.e.:
   $("#items").val(id);
});
Run Code Online (Sandbox Code Playgroud)