Yii2:如何在输入字段前后添加符号

Ale*_*dra 2 html css php yii2

如何在输入文本前后添加符号?就像图像附加一样。

在此处输入图片说明

我的代码:

<?= $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']) ?>
Run Code Online (Sandbox Code Playgroud)

解决方案

我按照 ActiveForm 文档修改了我的代码并且它有效!

代码修改:

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

小智 7

例如,您可以使用 ActiveForm 字段"template"选项向输入添加一些插件。

例如%输入末尾的插件:

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