【问题标题】:select2 widget selected will updated other select2选定的 select2 小部件将更新其他 select2
【发布时间】:2019-10-01 17:22:28
【问题描述】:

大家好,我是 yii2 的新手,需要您的建议 我使用 select2 小部件,当我选择值时,它会抛出我之前在数组中设置的另一个值。那么我如何在 yii2 中做到这一点。到目前为止,我这样做了。我尝试在 select2 中使用 pluginevents 使用 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 谢谢帮忙

【问题讨论】:

  • 不清楚你在问什么你在这里谈论的是哪个数组?我从您的代码中了解到,当我们更改 select2 中的选项时,onchange 函数将获取当前选定的值,然后将该值插入到输入 target 中,该输入为文本类型。
  • $idpwp 是一个数组。数组内容 id,npwp,nama_wp .. 如果我在 npwp 表单中选择值,nama_wp 将填写在 nama_wp 表单字段中..
  • 看下面的答案

标签: yii2


【解决方案1】:

首先你需要改变

<?= $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的代码更改为以下。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2017-12-10
    • 2020-08-07
    • 1970-01-01
    • 2018-02-27
    相关资源
    最近更新 更多