Display of units in HTML stepper <input type=“number”>

瘦欲@ 提交于 2019-12-23 08:49:40

问题


I want the user to enter numbers with a unit such as "cm", "kg" or "$". This can be done in jQuery UI: Example

However, I would like to implement it in pure html, such as:

input{
  display: inline;
}

div.euro-sign::after{
  content: "€";
  margin-left: -40px;
}
<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 -->

Is there a more native way for doing it (like example 2) or do I have to implement the workaround (example 3)?


回答1:


$(".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));
});
.duplicate {
    position: relative;
    top: -20px;
    left: 2px;
    float: left;
}
.duplicate input {
    width: 145px;
    border: none;
}
<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>



回答2:


Proposing to use radio buttons...

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


来源:https://stackoverflow.com/questions/28605416/display-of-units-in-html-stepper-input-type-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!