Yii2-dropDownList填充textInput

时间:2018-08-26 23:45:39

标签: jquery yii2 active-form

我有这个视图:

<div class="col-xs-12 col-sm-12 col-lg-12">
   <?= $form->field($model, 'name')->dropDownList(['E' => 'Data Entry', 'S' => 'Mark Entry'], ['prompt' => 'Select Option']) ?>
</div>

<div class="col-xs-12 col-sm-12 col-lg-12">
   <?= $form->field($model, 'detail')->textInput(['maxlength' => 20, 'placeholder' => $model->getAttributeLabel('detail')]) ?>
</div>

我想要实现的是,当我单击dropDownList时,如果值为数据输入,则textInput将为数据输入。然后,如果值为标记输入,则textInput将为标记输入

我该如何实现?

1 个答案:

答案 0 :(得分:0)

您可以为每个输入设置id属性,以通过 jQuery 获得其值:

<div class="col-xs-12 col-sm-12 col-lg-12">
    <?= $form->field($model, 'name')->dropDownList(['E' => 'Data Entry', 'S' => 'Mark Entry'], ['id' => 'firstInput', prompt'=>'Select Option']) ?>
</div>

<div class="col-xs-12 col-sm-12 col-lg-12">
    <?= $form->field($model, 'detail')->textInput(['id' => 'secondInput', maxlength' => 20, 'placeholder' => $model->getAttributeLabel('detail')]) ?>
</div>

以及您的 jQuery 代码:

$('#firstInput').change(function() {
   var firstInputValue = $(this).val();
   $('#secondInput').val(firstInputValue);
});
相关问题