這是我到目前為止的代碼以及我希望輸入拒絕的限制功能
- 步長值為 0.1,即沒有小于 0.1 的值,即 0.01
- 沒有字符,即 w,r,t
- 由函式輸入設定的最小值,即 1
- 由函式輸入設定的最大值,即 5
- 請注意我撰寫其余代碼的方式,輸入表單的型別不能是 ="number" 它必須是 ="text"
任何幫助將不勝感激
function myFunction(e, low, high) {
console.log("called");
console.log("low" low);
console.log("high" high);
console.log(e.target.value e.key);
var charValue = String.fromCharCode(e.keyCode);
var nextValue = e.target.value e.key;
if (((!/^(\d )?([.]?\d{0,1})?$/.test(e.target.value e.key)) && (e.which != 8)) && (nextValue < 1 || nextValue > 5)) {
e.preventDefault()
}
}
<!DOCTYPE html>
<html>
<form name="addtext">
SetPoint :<input id="setPoint" type="text" name="setPoint" onkeydown="myFunction(event,3, 5)" /><br />
</form>
uj5u.com熱心網友回復:
除非您在玩 Code Golf,否則將所有邏輯塞進一個條件中是沒有獎勵的。
將條件拆分為多個if陳述句,以便您可以清楚地實作您的邏輯。
您需要更換硬編碼1,并5與low和high。
function myFunction(e, low, high) {
console.log("called");
console.log("low" low);
console.log("high" high);
var nextValue = e.target.value e.key;
console.log(nextValue);
if (e.which == 8) { // allow backspace
return;
}
if (!/^(\d )?([.]?\d{0,1})?$/.test(nextValue)) {
e.preventDefault(); // non-number, don't allow
}
if (nextValue < low || nextValue > high) {
e.preventDefault();
}
}
<!DOCTYPE html>
<html>
<form name="addtext">
SetPoint :<input id="setPoint" type="text" name="setPoint" onkeydown="myFunction(event,3, 5)" /><br />
</form>
uj5u.com熱心網友回復:
或者,您可以使用為此而制作的輸入。不需要 javascript。
<input id="range" value="1" type="range" step="0.1" min="0.1" max="5.0">
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/323690.html
標籤:javascript html 形式 输入
下一篇:回圈中的混亂
