简体   繁体   English

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

[英]How to get value of selected option of yii2 select2 widget

I am trying to get the value of selected option within same page. 我试图在同一页面中获取所选选项的值。 I am using select2 widget. 我正在使用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?> />'

Here I want get that selected value within that page in input box. 在这里,我要在输入框中获取该页面内的所选值。

<input type="text" id='items' value="<?=$selectedvalue?> />

I am using Yii2 I am new on it. 我正在使用Yii2,因此我是新手。 can anyone help me? 谁能帮我?

You can get the selected value with jQuery with: 您可以使用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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM