【问题标题】:Unable to reach input type range to max无法达到最大输入类型范围
【发布时间】:2021-08-02 08:22:52
【问题描述】:

我正在尝试达到 105 的最大值。但无法做到。有没有可能达到105。代码如下,

    <input type="range" min="0" step="10" max="105">

【问题讨论】:

  • 使用 step=10,您期望会发生什么?
  • 我期待是否有可能达到 105。但无法达到。在范围内,它仅移动到 100。
  • 您需要第 5 步。
  • 我们可以使用任何JS吗?因为我不能改变步骤。最大值是动态的。它可以是十进制值,例如 104.12

标签: html


【解决方案1】:

当然,只需将步长更改为 5:

<input type="range" min="0" step="5" max="105">

否则你将无法通过10步达到105:

105 % 10 = 5

你总是会得到剩下的五个,这是无法达到的(在你的意义上)。

【讨论】:

  • 是的,同意,但我无法更改步骤。最大值可以是十进制。我们可以使用任何JS吗?
  • 请看一下这个问题:stackoverflow.com/questions/60000964/… 这并不完全是您的情况,但它可能有助于找到解决方案。祝你好运!棘手的问题,你遇到了。
  • 谢谢,@Michael Czechowski,我可以将其用作临时解决方案。
【解决方案2】:

当您无法更改步长和起点时,您可以在最高点时将范围切换到 100% 吗?
你真正想要的是不可能的。即使手动设置输入也不起作用,因为在任何状态下,根据定义,输入都不是 105 作为有效值。
您可以添加较低的步率,然后通过 javascript 进行更正:

function correct(nValue, nMin, nStep, nMax, nCorrectStep){ // correct(int, int, int, int, int): int
console.log("Input: "+nValue); // DEBUG
    if(nValue == nMin+Math.floor((nMax-nMin)/nStep)*nStep){ // pull up to max value that is a valid value after html attributes but not a correct value after the else block below
        console.log("Output: "+nMax); // DEBUG
        return nMax;
    }
    else{
        console.log("Output: "+(nMin+Math.floor((nValue-nMin)/nCorrectStep)*nCorrectStep)); // DEBUG
        return nMin+Math.floor((nValue-nMin)/nCorrectStep)*nCorrectStep; // round down to next valid step by multiply nCorrectStep again after downward adjusted dividing with nCorrectStep
    }
}
&lt;input type="range" min="0" step="5" max="105" value="0" onchange="this.value = correct(parseInt(this.value), parseInt(this.min), parseInt(this.step), parseInt(this.max), 10);"/&gt;

【讨论】:

    猜你喜欢
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 2013-11-23
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    相关资源
    最近更新 更多