Use*_*419 94 html text field currency persistent
我想在一开始就有一个包含"$"符号的文本输入字段,无论字段发生什么编辑,标志都是持久的.
如果只接受输入数字,我会很好,但这只是一个奇特的补充.
Bal*_*usC 96
考虑使用带有无边界输入字段边框的跨度来模拟具有固定前缀或后缀的输入字段.这是一个基本的启动示例:
.currencyinput {
border: 1px inset #ccc;
}
.currencyinput input {
border: 0;
}
Run Code Online (Sandbox Code Playgroud)
<span class="currencyinput">$<input type="text" name="currency"></span>
Run Code Online (Sandbox Code Playgroud)
ryb*_*111 55
使用父.input-icon
div.可选择添加.input-icon-right
.
<div class="input-icon">
<input type="text">
<i>$</i>
</div>
<div class="input-icon input-icon-right">
<input type="text">
<i>€</i>
</div>
Run Code Online (Sandbox Code Playgroud)
使用transform
和垂直对齐图标top
,并设置pointer-events
为none
使点击对焦于输入.调整padding
和width
适当的:
.input-icon {
position: relative;
}
.input-icon > i {
position: absolute;
display: block;
transform: translate(0, -50%);
top: 50%;
pointer-events: none;
width: 25px;
text-align: center;
font-style: normal;
}
.input-icon > input {
padding-left: 25px;
padding-right: 0;
}
.input-icon-right > i {
right: 0;
}
.input-icon-right > input {
padding-left: 0;
padding-right: 25px;
text-align: right;
}
Run Code Online (Sandbox Code Playgroud)
与接受的答案不同,这将保留输入验证突出显示,例如出现错误时的红色边框.
带有Bootstrap和Font Awesome的JSFiddle用法示例
Phi*_*ann 28
您可以将输入字段包装到跨度中position:relative;
.然后使用:before
content:"€"
您的货币符号添加
并制作它position:absolute
.工作JSFiddle
HTML
<span class="input-symbol-euro">
<input type="text" />
</span>
Run Code Online (Sandbox Code Playgroud)
CSS
.input-symbol-euro {
position: relative;
}
.input-symbol-euro input {
padding-left:18px;
}
.input-symbol-euro:before {
position: absolute;
top: 0;
content:"€";
left: 5px;
}
Run Code Online (Sandbox Code Playgroud)
更新 如果要将欧元符号放在文本框的左侧或右侧.工作JSFiddle
HTML
<span class="input-euro left">
<input type="text" />
</span>
<span class="input-euro right">
<input type="text" />
</span>
Run Code Online (Sandbox Code Playgroud)
CSS
.input-euro {
position: relative;
}
.input-euro.left input {
padding-left:18px;
}
.input-euro.right input {
padding-right:18px;
text-align:end;
}
.input-euro:before {
position: absolute;
top: 0;
content:"€";
}
.input-euro.left:before {
left: 5px;
}
.input-euro.right:before {
right: 5px;
}
Run Code Online (Sandbox Code Playgroud)
Jef*_*han 18
既然你不能做::before
与content: '$'
投入和增加绝对定位的元素增加了额外的HTML -我喜欢做一个背景SVG内联CSS.
它是这样的:
input {
width: 85px;
background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='16px' width='85px'><text x='2' y='13' fill='gray' font-size='12' font-family='arial'>$</text></svg>");
padding-left: 12px;
}
Run Code Online (Sandbox Code Playgroud)
它输出以下内容:
注意:代码必须全部在一行上.在现代浏览器中支持相当不错,但一定要测试.
我最终需要类型仍然保留为数字,因此使用 CSS 使用绝对位置将美元符号推入输入字段。
<div>
<span style="position: absolute; margin-left: 1px; margin-top: 1px;">$</span>
<input type="number" style="padding-left: 8px;">
</div>
Run Code Online (Sandbox Code Playgroud)