Yii2 - 下拉列表以填充文本输入



我有这个观点:

<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时,如果值是数据输入,那么文本输入将是数据输入。然后,如果值为"标记条目",则文本输入将为"标记条目"。

我如何实现这一点?

您可以为每个输入设置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);
});

最新更新