Select2 Krajee 小部件 - 使用模态并获取值的 id

Select2 Krajee widget - using modal and getting the id of the value

使用模态获取select项的值的方法是什么。如演示示例所示(在模态对话框中使用 select2 小部件):

Modal::begin([
'options' => [
    'id' => 'kartik-modal',
    'tabindex' => false // important for Select2 to work properly
],
'header' => '<h4 style="margin:0; padding:0">Select2 Inside Modal</h4>',
'toggleButton' => ['label' => 'Show Modal', 'class' => 'btn btn-lg btn-primary'],
]);


echo Select2::widget([
'name' => 'state_40',
'data' => $data,
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions' => [
    'allowClear' => true
],
]);
Modal::end();

我得到了要显示的选项和 select,但我需要将 selected 值的 ID 保存在某处,以便我可以将其作为 [= 的一部分转发19=] 在按钮中。

例如:

Html::button('Example', ['value' => Url::to(['example/example', 'id' => ?]),'class' => 'btn btn-lg btn-primary ']);

这是一个简单的方法。我做过很多次了。

'options' => [
                'class'=>'col-md-3','id'=>'id_select',
                'placeholder' => 'Select the country',
             ],

与 jquery :

var id_select = $('#id_select').val();

或者如果你喜欢 ajax:

      echo  Html::a('<i class="fa fa-check"></i> Send',null,[
    'class' => 'btn btn-primary',
    'title' => Yii::t('yii', 'Enviar'),
    'onclick'=>"
     var id_select = $('#id_select').val();
     $.ajax({
        type:'post',
        cache    : false,
        url  : '".Url::toRoute(['example'])."&id='+id_select,
        success  : function(response) {
          //ok      
        },
        error: function() {
          alert('Error');
        }
     });
]);

您无法使用 php 获取所有参数,您需要客户端语言来获取此参数。 但是,我邀请您使用 <form> 模型,更简单快捷。