选择的 select2 小部件将更新其他 select2 [英] select2 widget selected will updated other select2

查看:39
本文介绍了选择的 select2 小部件将更新其他 select2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是 yii2 的新手,需要您的建议我使用 select2 小部件,当我选择值时,它会抛出我之前在数组中设置的另一个值.那么我如何在 yii2 中做到这一点.到目前为止,我正在这样做.我尝试在 select2 中使用 pluginevents 使用 jquery 函数,但仍然卡住了..这是我的代码

Hi all i am newbie in yii2 need your advise i use select2 widget, when i selected value it will throw another value that i had set in array before. so how can i do this in yii2. so far i doing this.i have try using jquery function using pluginevents in select2 but still stuck..here is my code

<?= $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 字段中感谢帮助

how can i insert 'nama_wp' that already set in array into field nama_wp thx for helping

推荐答案

你需要改变的第一件事

First thing you need to change the

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

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

需要通过data.currentTarget.value访问select2的当前值,所以将change的代码改成如下.

The you need to access the current value of the select2 via data.currentTarget.value, so change the code for change to the following.

<?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 );
        }',
    ]
]);  
?>

这篇关于选择的 select2 小部件将更新其他 select2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆