簡體   English   中英

如何獲取yii2 select2小部件的選定選項的值

[英]How to get value of selected option of yii2 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?> />'

在這里,我要在輸入框中獲取該頁面內的所選值。

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

我正在使用Yii2,因此我是新手。 誰能幫我?

您可以使用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