![](/img/trans.png)
[英]How do I store a user prompt(input) into an array and retrieve it's max and min value?
[英]How to set input to it's min/max threshold if user input exceeds it
我有一個輸入類型“數字”,最小為 1,最大為 99。我知道,如果用戶輸入的數字高於 99 或低於 1,則會出現提示,告訴他們輸入數字 1-99。 我想要它,所以如果用戶輸入的數字超出了最小值/最大值,它會自動將自身調整為最小值/最大值,並且不會向用戶提示此消息。
示例:用戶輸入 107,數字變為 99。如果用戶輸入 -10,則數字變為 1。
我對此有一個思考過程:
如果 number 大於 99 則將 number 變為 99
如果 number 小於 1 則將 number 變為 1
雖然,我不確定如何在 JavaScript 中寫這個(假設我必須使用 JS)。
您可以通過在每次更改時檢查值來做到這一點
var numInput = document.querySelector("input[type='number']"); numInput.onchange = function() { this.value = this.value > 99? 99: this.value < 1? 1: this.value; }
<input type="number">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.