[英]Comma separated step for number input box
當用戶輸入數字時,我可以以千為增量指定“步長”。 但是,為什么我不能寫“ 1,000”而不是“ 1000”,以使成千上萬的逗號分隔出現增量? 這個怎么做?
<div class="input"><label for="salary">Salary</label> <input class='inp_cont' id="salary" name="salary" placeholder="Enter your salary" step="1000" min="0" required="" type="number"></div>
如果您要堅持type="number"
,則無法完成此操作,因為:
根據Chrome的報告:
該值必須與以下正則表達式匹配:-?(\\ d + | \\ d +。\\ d + |。\\ d +)([eE] [-+]?\\ d +)?
如規范所述:
值= 浮點數
單擊上面的鏈接,可以看到規范根本沒有提到逗號的使用。
如果要切換到type="text"
:
document.getElementById('salary').addEventListener('input', event => event.target.value = (parseInt(event.target.value.replace(/[^\\d]+/gi, '')) || 0).toLocaleString('en-US') );
<div class="input"> <label for="salary">Salary</label> <input class='inp_cont' id="salary" pattern="^[\\d,]+$" name="salary" placeholder="Enter your salary" required="" type="text"> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.