【问题标题】:HTML text input field with currency symbol带有货币符号的 HTML 文本输入字段
【发布时间】:2011-02-24 04:31:51
【问题描述】:

我希望有一个在开头包含“$”符号的文本输入字段,并且无论对该字段进行什么编辑,都可以使该符号保持不变。

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

【问题讨论】:

    标签: html text field currency persistent


    【解决方案1】:

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

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

    【讨论】:

    • 另外,您可以将“$”包装在 时,这会将焦点移到输入字段
    • @Cohen 虽然是个好主意,但拥有多个标签may cause problems with accessibility
    【解决方案2】:

    使用父级.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>
    

    将图标与transformtop 垂直对齐,并将pointer-events 设置为none,以便点击聚焦于输入。适当调整paddingwidth

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

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

    JSFiddle usage example with Bootstrap and Font Awesome

    【讨论】:

    • 我想在一行中添加一个按钮,并且必须将 float:left 添加到 input-symbol div
    • @rybo111 不,我的意思是input-symbol 容器之外的单独按钮
    • 你说得对,float 很糟糕,所以我现在正在使用display:inline-blockvertical-align:bottom :) 虽然我不能快速做出调整,但我的其他 CSS 太多了当前示例 ;-)
    • 我喜欢这个答案而不是接受的答案,因为这(正如作者提到的)保留了引导程序的验证行为,这很棒!感谢您的帮助。
    • @AndreiCleland 点击左侧的“Resources”,你会看到加载的 CSS 和 JS 文件。如果你的意思是我不使用任何 Bootstrap HTML - 我使用的是 form-* 类。
    【解决方案3】:

    您可以将输入字段包装到一个跨度中,您可以使用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;
     }
    

    【讨论】:

    • 这非常接近我想要的,但是输入在页面宽度上不是固定的,不能使用绝对值。相对也会移动符号。我们如何设置绝对/相对于输入左上角的位置?
    • 欧元货币始终显示在右侧。你应该把你的符号放在右边而不是左边。
    • 我是法国人,价格总是写成 2.50 欧元而不是 2.50 欧元,我很确定欧元区的其他国家也会这样做。
    • @jadok 我住在荷兰,这里的价格写成 2.50 欧元。
    • @VincentKok 在四处询问之后似乎取决于国家,拉丁欧洲国家(法国,西班牙,...)将把它放在右边,北欧/东欧国家放在左边,您可以通过 amazone 进行尝试:www.amazone.fr, www.amazone.nl, ...
    【解决方案4】:

    由于您不能在输入上使用 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,或者浏览器可能会呈现小部件本身而不是使用系统- 提供的小部件。
    • 我也将这个想法用于百分号,并在输入中添加了一个数据类型属性,并在嵌入的 svg 上添加了一个样式标签:input[data-type='percentage'] { background-image: url("data:image/svg+xml;utf8,&lt;svg xmlns='http://www.w3.org/2000/svg' version='1.1' height='auto' style='margin-top:9px;margin-left:45px' width='85px'&gt;&lt;text x='2' y='13' fill='gray' font-size='12' font-family='arial'&gt;%&lt;/text&gt;&lt;/svg&gt;"); background-repeat: no-repeat; }
    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

      【讨论】:

      • 是的,我实际上希望货币符号位于字段内,但感谢输入限制链接!
      【解决方案7】:

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

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

      【讨论】:

      • 谢谢!我知道这一点,但实际上我需要将货币符号放在文本字段内。下面有一个更好的答案,我相信它可以对很多人有所帮助。
      • @Hristo:很高兴您找到了适合的解决方案。但是为了记录,您标记为答案的那个并没有将您的 $ in 输入字段。它只会让样式看起来在那里。也就是说,它绝对是光滑的!
      • 是的,它在视觉上解决了问题,而您的建议只是指向(是的,仍然是正确的方向),但我不会称之为解决方案。再次非常感谢你,记住我不是故意冒犯这里的任何人!
      【解决方案8】:

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

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

      和像

      这样的输入字段

      &lt;input class="currency" data-symbol="€" type="number" value="12.9"&gt;

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

      【讨论】:

      【解决方案9】:

      试试这个

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

      【讨论】:

        【解决方案10】:

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

        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

        【讨论】:

          【解决方案11】:

          为了引导它的工作

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

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

          【讨论】:

            【解决方案12】:

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

            【讨论】:

            • 没有解释的代码对 OP 或这个答案的未来观众几乎没有帮助。您应该解释这是如何回答问题的。
            【解决方案13】:

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

            我建议将美元符号绝对定位在左侧约 -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>
            

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

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

            【讨论】:

            • 所有答案都无济于事,因为这不是问题所要求的。该符号应该是 inside 输入。
            • 从技术上讲,css 答案也没有将美元符号放在输入容器中。我认为公认的 css 解决方案在应用于典型表单时会引入复杂的对齐问题。如此处所示:plnkr.co/edit/uhOfLw5WB6DIn2le9GZm?p=preview
            • 我已更新此解决方案,将美元符号放在输入中:plnkr.co/edit/yshyuRMd06K1cuN9tFDv?p=preview(绝对位置左 5px 并将输入方向设置为 rtl)
            【解决方案14】:

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

            input{ 
               margin-left: 20px;
             }
            input:before {
              content: "$";
              position: absolute;
            }
            

            【讨论】:

            • 我无法在 Chrome 中重现此内容。您可以发布一个有效的 JSFiddle 示例吗?
            • 输入不支持 CSS3 中的伪元素。换句话说,纯 CSS 是不可能的。但是,如果使用 jquery,您可以使用 $("input").after("$");
            【解决方案15】:

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

            %

            【讨论】:

              【解决方案16】:

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

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

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

              【讨论】:

                【解决方案17】:
                .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>
                

                【讨论】:

                  猜你喜欢
                  • 2016-01-02
                  • 2019-11-17
                  • 1970-01-01
                  • 2013-07-03
                  • 2022-01-07
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-12-03
                  相关资源
                  最近更新 更多