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 に答える
2

'$' をテキスト入力フィールドの中ではなく前に置きます。送信後に「$」を解析する必要がないため、数値データの検証が非常に簡単になります。

JQuery.validate () (またはその他) を使用して、通貨を処理するクライアント側の検証規則を追加できます。これにより、入力フィールド内に「$」を含めることができます。ただし、セキュリティのためにサーバー側の検証を行う必要があるため、「$」を削除する必要があります。

于 2010-05-26T13:22:02.140 に答える
2
 $<input name="currency">

参照:テキストボックスへの入力を制限する: 数字と小数点のみを許可する

于 2010-05-26T13:19:46.263 に答える
1

私が好むもう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 に答える
-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 に答える