我希望用户输入一个单位为“cm”,“kg”或“$”的数字.这可以在jQuery UI:
Example中完成
但是,我想在纯HTML中实现它,例如:
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 -->
是否有更原生的方式(如示例2)或我是否必须实施变通方法(示例3)?
解决方法
$(".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>