yii2在';其他';在下拉列表中选择选项
我的表单中有一个下拉列表,我想让用户能够选择“其他”,这将使他能够在文本框中插入一个值,而不是下拉列表中的值。 可能吗yii2在';其他';在下拉列表中选择选项,yii2,Yii2,我的表单中有一个下拉列表,我想让用户能够选择“其他”,这将使他能够在文本框中插入一个值,而不是下拉列表中的值。 可能吗 <?= $form->field($model, 'institution')->dropDownList(ArrayHelper::map(Institution::find()->all(), 'iId', 'name'), ['class'=>'form-control inline-block']); ?> 我想和放射科医
<?= $form->field($model, 'institution')->dropDownList(ArrayHelper::map(Institution::find()->all(), 'iId', 'name'), ['class'=>'form-control inline-block']); ?>
我想和放射科医生做同样的事情
<?= $form->field($model, 'selfDescription')->radioList(array('good'=>'good','very good'=>'very good','best'=>'best')); ?>
谢谢。首先。将“其他”选项添加到dropDownList。并使用JS在前端处理它
$form
->字段($model,‘institution’)
->下拉列表(
ArrayHelper::map(Institution::find()->all(),
“iId”,
“姓名”
)+[“其他”=>“其他”],
[
'class'=>'form-control内联块',
//下一代码在选择除“其他”选项以外的任何选项时禁用文本字段
//适用于jQuery1.6+
'onchange'=>新的JsExpression('
$(“#textFieldSelector”).prop(“disabled”、$(this).find(“option:selected”).val()!=“other”),
]
);
然后,您需要正确设置验证器。加上
['textField','required','when'=>函数($model){
返回$model->institution==“其他”;
}]
可能是。创建一个文本框并隐藏它。当使用选择其他下拉时,使用jQuery显示文本框。考虑使用一些增强的列表框小部件,如Selectize或Saltt2。他们可能已经做了你需要的事情。