在HTML步进器<input type =“number”/>中显示单位

时间:2015-02-19 11:40:21

标签: html5 html-form html-input

我希望用户输入一个单位,例如&#34; cm&#34;,&#34; kg&#34;或&#34; $&#34;。这可以在jQuery UI中完成:Example

enter image description here

但是,我想在纯HTML中实现它,例如:

&#13;
&#13;
input{
  display: inline;
}

div.euro-sign::after{
  content: "€";
  margin-left: -40px;
}
&#13;
<div><input placeholder="5 €" type="number" step="1"></div>

<div><input placeholder="5 €" type="number" step="1" unit="€"></div><!-- NOT working -->

<div class="euro-sign"><input placeholder="5" type="number" step="1"></div><!-- Workaround -->
&#13;
&#13;
&#13;

是否有更原生的方式(如示例2)或我是否必须实施变通方法(示例3)?

2 个答案:

答案 0 :(得分:3)

&#13;
&#13;
$(".original input").on("change", function(){
    $(".duplicate input").val(this.value + '€');
});
$(".duplicate input").on("change", function(){
    $(".original input").val(this.value.substring(0, this.value.length - 1));
});
&#13;
.duplicate {
    position: relative;
    top: -20px;
    left: 2px;
    float: left;
}
.duplicate input {
    width: 145px;
    border: none;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="euro-sign">
    <div class="original">
        <input type="number" step="1"/>
    </div>
    <div class="duplicate">
        <input type="text" value="0€"/>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

建议使用单选按钮......

<div>
<input type="radio" name="Money " value="YEN">YEN ¥ <input placeholder=" 0" type="number" name="HowMuch " step="5" min="0">
</div><br>

<div>
<input type="radio" name="Money " value="USD" checked>US $ <input placeholder=" 0" type="number" name="HowMuch " step="5" min="0">
</div><br>

<div>
<input type="radio" name="Money " value="EURO">EUR € <input placeholder=" 0" type="number" name="HowMuch " step="5" min="0">
</div><br>

<input type="submit">