Yii2: How add a symbol before and after an input field

大城市里の小女人 提交于 2019-12-11 08:51:29

问题


How can I add a symbol before and after of an input text? Just like the image attach.

My code:

<?= $form->field($model_product, 'percent')->textInput(['class'=>'smallInputFormInline'])->label('Percent',['class'=>'labelModalFormInline']) ?>

<?= $form->field($model_product, 'percent_all')->textInput(['class'=>'smallInputFormInline'])->label('Percent All',['class'=>'labelModalFormInline']) ?>

<?= $form->field($model_product, 'amount')->textInput(['class'=>'smallInputFormInline'])->label('Amount',['class'=>'labelModalFormInline']) ?>

SOLUTION

I modify my code following ActiveForm documentation and it works!

Code modified:

<?= $form->field($model_product, 'amount', ['template' => '{label}${input}'])->textInput(['class'=>'smallInputFormInline'])->label('Amount',['class'=>'labelModalFormInline']) ?>

回答1:


For example you can add some addons to input using ActiveForm field "template" option.

E.g. % addon at the end of input:

<?= $form->field($model_product, 'percent', [
            'template' => '{beginLabel}{labelTitle}{endLabel}<div class="input-group">{input}
            <span class="input-group-addon">%</span></div>{error}{hint}'
        ]); ?>



回答2:


Using pseudo classes we can do.

.input-symbol {
  position: relative;
  padding: 20px;
}
.input-symbol:before {
  content: "\0024";
  position: absolute;
  left:0;
  
}
.input-symbol:after {
  content: "\0024";
  position: absolute;
}
<div class="input-symbol">
  <input type="text">
</div>

Appreciate if it is useful :)



来源:https://stackoverflow.com/questions/47140005/yii2-how-add-a-symbol-before-and-after-an-input-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!