简体   繁体   中英

How to add dollar sign to input field without affecting the value of input?

如何将$添加到以下输入中:

<td class="td10"><input type='text' class='form-control' id="boqRate" name="boq[boqRate]"></td>

Try this. I have added span element with position:absolute .

 .prefix { margin-right: -10px; position: absolute; } .my-input { padding-left: 5px; } 
 <td class="td10"><span class="prefix">$</span><input class="my-input" type='text' class='form-control' id="boqRate" name="boq[boqRate]"></td> 

This should work:

 let input = document.getElementById("name"); input.addEventListener("keydown", () => { let inputValue = input.value; if (inputValue) { if (inputValue.charAt(0) === "$") { inputValue = inputValue.substring(1); } } let newValue = `$${inputValue}`; input.value = newValue; }); 
 <input id="name" /> 

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM