简体   繁体   English

如何设置“最大值”<input> HTML 中的标签?

[英]How to set 'max' value in <input> tag in HTML?

Can I set 'max' value in我可以在

            <input type="number" step="10" min="10"
                   th:max=""
                   th:field="*{coins}" required>

with variable created in ?变量创建于 ?

        <script>
            var wallet = [[${session.student.wallet}]];
            var price = [[${item.itemCost}]];
            var lowerValue = Math.min(wallet, price);
        </script>

The problem is to assign to 'max' the smaller of the values visible in .问题是将 .max 中可见的较小值分配给 'max'。

If you give your input an id attribute like (<input id="my-input"...), then it should be as simple as:如果你给你的输入一个id属性,比如 (<input id="my-input"...),那么它应该很简单:
document.getElementById("my-input").setAttribute("max", myCalculatedValue) ; 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" />

 //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;">

You can do this with just Thymeleaf.你可以只用 Thymeleaf 来做到这一点。

<input type="number"
       step="10" min="10"
       th:max="${T(Math).max(session.student.wallet, item.itemCost)}"
       th:field="*{coins}"
       required>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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