3 回答

TA貢獻1866條經驗 獲得超5個贊
如果你給你的輸入一個id像 (<input id="my-input"...) 這樣的屬性,那么它應該像這樣簡單
document.getElementById("my-input").setAttribute("max", myCalculatedValue):
let
wallet = 50.00,
price = 42.00;
const myCalculatedValue = Math.min(wallet, price);
const myInput = document.getElementById("my-input");
myInput.setAttribute("max", myCalculatedValue);
<input id="my-input" type="number" step="10" min="10" />

TA貢獻1821條經驗 獲得超6個贊
//default the max to 99
document.getElementById("yourinput").max = 99;
// setMax gets triggered by the button's `onclick` attribute
function setMax() {
let max = document.getElementById("yourmax").value;
document.getElementById("yourinput").max = max;
}
<!-- Note that I gave your elements IDs so JS can reference them -->
Default <b>max=99</b> got set when page loaded. Try it below.<br/>
Set max here: <input id="yourmax" value="10"><br/>
Click this button to apply your NEW max: <button onclick="setMax()">Set Max</button></br>
Max gets set on this input: <input id="yourinput" type="number" min="0" style="width:50px;">

TA貢獻1784條經驗 獲得超8個贊
您只需使用 Thymeleaf 即可做到這一點。
<input type="number" step="10" min="10" th:max="${T(Math).max(session.student.wallet, item.itemCost)}" th:field="*{coins}" required>
添加回答
舉報