Yii2 选择的select2小部件将更新其他select2

Yii2 选择的select2小部件将更新其他select2,yii2,Yii2,大家好,我是yii2的新手,需要你们的建议吗 我使用select2小部件,当我选择值时,它将抛出我以前在数组中设置的另一个值。那么我怎样才能在yii2中做到这一点呢。到目前为止,我正在做这件事。我已经尝试使用select2中的PlugineEvents使用jquery函数,但仍然卡住了。这是我的代码 <?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?> <?= $for

大家好,我是yii2的新手,需要你们的建议吗 我使用select2小部件,当我选择值时,它将抛出我以前在数组中设置的另一个值。那么我怎样才能在yii2中做到这一点呢。到目前为止,我正在做这件事。我已经尝试使用select2中的PlugineEvents使用jquery函数,但仍然卡住了。这是我的代码

<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
<?= $form->field($model, 'npwp')->widget(Select2::classname(), [
    'language' => 'id',
    'data' => $idnpwp,
    'options' => ['placeholder' => 'Select a NPWP ...'],
    'pluginOptions' => [
    'allowClear' => true
    ],
    'pluginEvents' => [
        "change" => 'function(data) { 
            var data_id = $(this).val();
            $("input#target").val($(this).val());
        }',
    ]
]);  
?>

<?= $form->field($model, 'nama_wp')->textInput(['id' => 'target']) ?>

如何将数组中已设置的“nama_wp”插入字段nama_wp
感谢thx的帮助

您需要更改

<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>

不清楚您在问什么,您在这里谈论的是哪个阵列?我从您的代码中了解到,当我们更改select2中的选项时,onchange函数将获得当前选定的值,然后将该值插入文本类型的输入
目标
。$idpwp是一个数组。数组内容id、npwp、nama_wp。。如果我在npwp表格中选择值,nama_wp将在nama_wp表格字段中填写。请参见下面的答案
<?php $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
<?php echo $form->field($model, 'npwp')->widget(Select2::classname(), [
    'language' => 'id',
    'data' => $idnpwp,
    'options' => ['placeholder' => 'Select a NPWP ...'],
    'pluginOptions' => [
    'allowClear' => true
    ],
    'pluginEvents' => [
        "change" => 'function(data) { 
            var data_id = data.currentTarget.value;
            $("#target").val(data_id );
        }',
    ]
]);  
?>