关于html:为什么我不能在HTML5数字字段中输入500? | 珊瑚贝

Why can’t I enter 500 in a HTML5 number field?


我的 HTML5 数字字段不断收到奇怪的意外验证错误。这是我的 HTML 代码:

1
<input type=”number” name=”width” maxlength=”5″ placeholder=”Width” value=”733.95591182365″ />Width<br />

当我输入 500 并提交表单时,我收到错误:”请输入一个有效值。两个最接近的有效值是 499.95591182365 和 500.95591182365。”

我已经解决了这个问题。问题是这个 value=”733.95591182365″。我已将我的 PHP 代码更改为将小数四舍五入为整数,以便只有整数可以回显到数字字段中。现在我只在输入小数时收到错误,这没关系。

  • maxlength 不适用于数字输入。


在使用 type=”number”

时,您需要将 step=”any” 作为属性

1
2
3
<form>
  <input step=”any” type=”number” name=”width” maxlength=”5″ placeholder=”Width” value=”733.95591182365″ />
</form>
  • 我已将您的答案标记为正确,因为这也可以,但我通过在回显之前将宽度四舍五入为整数解决了这个问题。
  • 那个 step=”any” 应该是 type=”number” 的默认值。谢谢!


对于数字输入,

The step scale factor is 1. The default step is 1

那么,由于没有step属性,所以允许的值步长是1?—1 = 1:

  • If the attribute is absent, then the allowed value step is the
    default step multiplied by the step scale factor.
  • 步长为 733.95591182365:

  • If the element has a value content attribute, and the result of applying the algorithm to convert a string to a number to the
    value of the value content attribute is not an error, then
    return that result and abort these steps.
  • 因此,

    Constraint validation: When the element has an allowed value step, and the result of applying the algorithm to convert a
    string to a number to the string given by the element’s
    value is a number, and that number subtracted from the step
    base is not an integral multiple of the allowed value step,
    the element is suffering from a step mismatch.

    由于 733.95591182365 – 500 = 233.95591182365 不是 1 的倍数,因此违反了该约束。

    如果您想要另一个步骤,请指定一个 step 属性。如果您不想执行任何步骤,请使用 step =”any”.


    来源:https://www.codenong.com/36167599/

    微信公众号
    手机浏览(小程序)
    0
    分享到:
    没有账号? 忘记密码?