作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的表单中有一个下拉列表,我想让用户选择“其他”选项,这样他就可以在文本框中插入一个值,而不是下拉列表中的值。这可能吗?
<?= $form->field($model, 'institution')->dropDownList(ArrayHelper::map(Institution::find()->all(), 'iId', 'name'), ['class'=>'form-control inline-block']); ?>
我想用 radioList 做同样的事情。
<?= $form->field($model, 'selfDescription')->radioList(array('good'=>'good','very good'=>'very good','best'=>'best')); ?>
谢谢。
最佳答案
首先。向 dropDownList 添加“其他”选项。并在您的前端使用 JS 处理它
$form
->field($model, 'institution')
->dropDownList(
ArrayHelper::map(Institution::find()->all(),
'iId',
'name'
) + ['other' => 'Other'],
[
'class'=>'form-control inline-block',
// next code disables text field when any but "other" option is selected
// works for jQuery 1.6+
'onchange' => new JsExpression('
$("#textFieldSelector").prop("disabled", $(this).find("option:selected").val() != "other");'),
]
);
然后您需要正确设置您的验证器。只需添加
['textField', 'required', 'when' => function($model) {
return $model->institution === 'other';
}]
关于yii2 在下拉列表中选择 'other' 选项时插入一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33060890/
我是一名优秀的程序员,十分优秀!