121

我希望在最开始时包含一个包含“$”符号的文本输入字段,并且无论对该字段进行什么编辑,该符号都是持久的。

如果只接受数字作为输入,我会很好,但这只是一个花哨的补充。

4

17 回答 17

110

考虑使用在无边框输入字段周围有边框的跨度来模拟具有固定前缀或后缀的输入字段。这是一个基本的启动示例:

.currencyinput {
    border: 1px inset #ccc;
}
.currencyinput input {
    border: 0;
}
<span class="currencyinput">$<input type="text" name="currency"></span>

于 2010-05-26T13:28:49.143 回答
76

使用父.input-icondiv。可选地添加.input-icon-right.

<div class="input-icon">
  <input type="text">
  <i>$</i>
</div>

<div class="input-icon input-icon-right">
  <input type="text">
  <i>€&lt;/i>
</div>

将图标与transform和垂直对齐top,并设置pointer-eventsnone使点击聚焦在输入上。适当调整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;
}

与接受的答案不同,这将保留输入验证突出显示,例如出现错误时的红色边框。

带有 Bootstrap 和 Font Awesome 的 JSFiddle 使用示例

于 2013-05-17T14:10:13.460 回答
34

您可以将输入字段包装到一个跨度中,您可以将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;
 }
于 2015-02-05T06:55:29.800 回答
28

由于您无法::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;
}

它输出以下内容:

svg 美元符号 背景 css

注意:代码必须都在一行。在现代浏览器中支持非常好,但一定要测试。

于 2015-12-18T07:54:38.727 回答
5

我最终需要将类型仍然保留为数字,因此使用 CSS 将美元符号推入使用绝对位置的输入字段。

<div>
   <span style="position: absolute; margin-left: 1px; margin-top: 1px;">$</span>
   <input type="number" style="padding-left: 8px;">
</div>

于 2018-10-12T17:36:21.357 回答
2
 $<input name="currency">

另请参阅:限制对文本框的输入:仅允许数字和小数点

于 2010-05-26T13:19:46.263 回答
2

将“$”放在文本输入字段的前面,而不是里面。它使数字数据的验证变得更加容易,因为您不必在提交后解析出“$”。

您可以使用JQuery.validate()(或其他)添加一些处理货币的客户端验证规则。这将允许您在输入字段中包含“$”。但是您仍然需要为安全性进行服务器端验证,这会使您回到必须删除“$”的位置。

于 2010-05-26T13:22:02.140 回答
2

如果你只需要支持 Safari,你可以这样做:

input.currency:before {
  content: attr(data-symbol);
  float: left;
  color: #aaa;
}

和一个输入字段

<input class="currency" data-symbol="€" type="number" value="12.9">

这样您就不需要额外的标签并将符号信息保留在标记中。

于 2013-05-31T00:06:34.453 回答
2

试试这个

<label style="position: relative; left:15px;">$</label>
<input type="text" style="text-indent: 15px;">

于 2020-09-24T05:46:03.587 回答
1

我更喜欢的另一个解决方案是为货币符号的图像使用额外的输入元素。以下是在搜索文本字段中使用放大镜图像的示例:

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;
}

这将导致此处左侧边栏中的输入:

https://fsfe.org

于 2012-01-19T17:07:15.280 回答
0

引导它的工作

<span class="form-control">$ <input type="text"/></span>

不要在输入字段中使用 class="form-control"。

于 2016-06-14T12:09:24.287 回答
0

如果您关心将左边框与输入表单上的其他文本字段对齐,这些答案都没有真正的帮助。

我建议将美元符号绝对定位在左侧约 -10 像素或左侧 5 像素(取决于您希望它在输入框内部还是外部)。内部解决方案需要输入 css 上的方向:rtl。

您还可以向输入添加填充以避免方向: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>

https://i.imgur.com/ajrU0T9.png

示例:https ://plnkr.co/edit/yshyuRMd06K1cuN9tFDv?p=preview

于 2018-05-22T15:36:47.153 回答
0

我只是在输入中使用 :before 并将 $ 作为内容传递

input{ 
   margin-left: 20px;
 }
input:before {
  content: "$";
  position: absolute;
}
于 2019-05-10T18:53:11.430 回答
0

<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>

%

于 2019-08-19T11:23:02.623 回答
-1

.currencyinput {
    border: 1px inset #ccc;
    padding-bottom: 1px;//FOR IE & Chrome
}
.currencyinput input {
    border: 0;
}
<span class="currencyinput">$<input type="text" name="currency"></span>

于 2016-10-19T20:30:58.623 回答
-1

是的,如果您使用引导程序,这将起作用。

.form-control input {
    border: none;
    padding-left: 4px;
}

<span class="form-control">$ <input type="text"/></span>
于 2018-09-04T14:41:08.093 回答
-1
.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>
于 2019-08-19T11:14:57.130 回答