将输入字段的最大值设置为标签的值

Setting the max value of an input field to the value of a label

本文关键字:标签 设置 最大值 输入 字段      更新时间:2023-09-26

我有一个输入字段(type = number)和一个标签。我试图将输入字段的max属性设置为标签的值。

我已经尝试了不同的方法,但都无济于事。

1)

<input id="Input1" type="number" step="any" min="1" max='<%#Convert.ToDecimal(Label1.InnerText) %>'>

2)

function setMaxTonnes() {
    var input = document.getElementById("<%= Input1.ClientID %>");
    var maxValue = document.getElementById("<%= Label1.ClientID %>").innerText;
    input.setAttribute("max", maxValue);
}
<input type="number" step="any" min="1" onfocus="setMaxTonnes()">

这两种操作都不会返回错误,但这并不妨碍我输入大于Label1中存储的值。

你知道我哪里错了吗?

您确定asp变量在编译标记中是正确的输出吗?试着用DevTools检查输入元素是否得到了你的最大值。你的例子看起来很准确,<input type="number" step="any" min="1" max="12">工作得很好。