【发布时间】:2011-02-24 04:31:51
【问题描述】:
我希望有一个在开头包含“$”符号的文本输入字段,并且无论对该字段进行什么编辑,都可以使该符号保持不变。
如果只接受数字作为输入就好了,但这只是一个花哨的补充。
【问题讨论】:
标签: html text field currency persistent
我希望有一个在开头包含“$”符号的文本输入字段,并且无论对该字段进行什么编辑,都可以使该符号保持不变。
如果只接受数字作为输入就好了,但这只是一个花哨的补充。
【问题讨论】:
标签: html text field currency persistent
考虑使用在无边框输入字段周围有边框的跨度来模拟具有固定前缀或后缀的输入字段。这是一个基本的启动示例:
.currencyinput {
border: 1px inset #ccc;
}
.currencyinput input {
border: 0;
}
<span class="currencyinput">$<input type="text" name="currency"></span>
【讨论】:
使用父级.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>
将图标与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;
}
与接受的答案不同,这将保留输入验证突出显示,例如出现错误时的红色边框。
【讨论】:
float:left 添加到 input-symbol div
input-symbol 容器之外的单独按钮
float 很糟糕,所以我现在正在使用display:inline-block 和vertical-align:bottom :) 虽然我不能快速做出调整,但我的其他 CSS 太多了当前示例 ;-)
您可以将输入字段包装到一个跨度中,您可以使用position:relative;。然后你添加
:before content:"€" 您的货币符号并使其成为position:absolute。工作JSFiddle
HTML
<span class="input-symbol-euro">
<input type="text" />
</span>
CSS
.input-symbol-euro {
position: relative;
}
.input-symbol-euro input {
padding-left:18px;
}
.input-symbol-euro:before {
position: absolute;
top: 0;
content:"€";
left: 5px;
}
更新 如果要将欧元符号放在文本框的左侧或右侧。工作JSFiddle
HTML
<span class="input-euro left">
<input type="text" />
</span>
<span class="input-euro right">
<input type="text" />
</span>
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;
}
【讨论】:
由于您不能在输入上使用 content: '$' 来执行 ::before 并添加绝对定位的元素会添加额外的 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;
}
它输出以下内容:
注意:代码必须全部在一行。在现代浏览器中支持非常好,但一定要测试。
【讨论】:
background-repeat: no-repeat;,因为 $ 在输入框中重复。
background-image,或者浏览器可能会呈现小部件本身而不是使用系统- 提供的小部件。
input[data-type='percentage'] { background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='auto' style='margin-top:9px;margin-left:45px' width='85px'><text x='2' y='13' fill='gray' font-size='12' font-family='arial'>%</text></svg>"); background-repeat: no-repeat; }
我最终需要将类型仍然保留为数字,因此使用 CSS 将美元符号推入使用绝对位置的输入字段。
<div>
<span style="position: absolute; margin-left: 1px; margin-top: 1px;">$</span>
<input type="number" style="padding-left: 8px;">
</div>
【讨论】:
$<input name="currency">
另请参阅:Restricting input to textbox: allowing only numbers and decimal point
【讨论】:
将“$”放在文本输入字段的前面,而不是里面。它使数字数据的验证变得更加容易,因为您不必在提交后解析出“$”。
您可以使用JQuery.validate()(或其他)添加一些处理货币的客户端验证规则。这将允许您在输入字段中包含“$”。但是您仍然需要为安全性进行服务器端验证,这使您重新回到必须删除“$”的位置。
【讨论】:
如果只需要支持Safari,可以这样:
input.currency:before {
content: attr(data-symbol);
float: left;
color: #aaa;
}
和像
这样的输入字段<input class="currency" data-symbol="€" type="number" value="12.9">
这样您就不需要额外的标签并将符号信息保留在标记中。
【讨论】:
试试这个
<label style="position: relative; left:15px;">$</label>
<input type="text" style="text-indent: 15px;">
【讨论】:
我更喜欢的另一个解决方案是为货币符号的图像使用额外的输入元素。以下是在搜索文本字段中使用放大镜图像的示例:
html:
<input type="image" alt="Subscribe" src="/graphics/icons/search-button.png">
<input type="text" placeholder="Search" name="query">
css:
#search input[type="image"] {
background-color: transparent;
border: 0 none;
margin-top: 10px;
vertical-align: middle;
margin: 5px 0 0 5px;
position: absolute;
}
这会导致左侧边栏的输入:
【讨论】:
为了引导它的工作
<span class="form-control">$ <input type="text"/></span>
不要在输入字段中使用 class="form-control"。
【讨论】:
.currencyinput {
border: 1px inset #ccc;
padding-bottom: 1px;//FOR IE & Chrome
}
.currencyinput input {
border: 0;
}
<span class="currencyinput">$<input type="text" name="currency"></span>
【讨论】:
如果您关心将左边框与输入表单上的其他文本字段对齐,这些答案都没有真正的帮助。
我建议将美元符号绝对定位在左侧约 -10 像素或左侧 5 像素(取决于您希望它位于输入框内部还是外部)。内部解决方案需要输入css上的direction:rtl。
您还可以在输入中添加填充以避免direction:rtl,但这会改变输入容器的宽度,使其与其他相同宽度的容器不匹配。
<div style="display:inline-block">
<div style="position:relative">
<div style="position:absolute; left:-10px;">$</div>
</div>
<input type='text' />
</div>
或
<div style="display:inline-block">
<div style="position:relative">
<div style="position:absolute; left:5px;">$</div>
</div>
<input type='text' style='direction: rtl;' />
</div>
【讨论】:
我只是在输入中使用 :before 并将 $ 作为内容传递
input{
margin-left: 20px;
}
input:before {
content: "$";
position: absolute;
}
【讨论】:
<style>
.currencyinput {
border: 1px inset #ccc;
border-left:none;
}
.currencyinput input {
border: 0;
}
</style>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="text" oninput="this.value = this.value.replace(/[^0-9]/.g, ''); this.value = this.value.replace(/(\..*)\./g, '$1');" style="text-align:right;border-right:none;outline:none" name="currency"><span class="currencyinput">%</span>
</body>
</html>
%
【讨论】:
是的,如果您使用引导程序,这将起作用。
.form-control input {
border: none;
padding-left: 4px;
}
和
<span class="form-control">$ <input type="text"/></span>
【讨论】:
.currencyinput {
border: 1px inset #ccc;
border-left:none;
}
.currencyinput input {
border: 0;`enter code here`
}
<input type="text" oninput="this.value = this.value.replace(/[^0-9]/.g, ''); this.value = this.value.replace(/(\..*)\./g, '$1');" style="text-align:right;border-right:none;outline:none" name="currency"><span class="currencyinput">%</span>
【讨论】: