简体   繁体   English

输入类型编号的属性“min”工作错误

[英]Attribute 'min' of input type number works wrong

I have an input type number with atrributes min,max,step; 我有一个输入类型编号,其中包含atrributes min,max,step;

In documentation https://developer.mozilla.org/ru/docs/Web/HTML/Element/Input/number says 在文档https://developer.mozilla.org/ru/docs/Web/HTML/Element/Input/number中

The minimum value to accept for this input. 接受此输入的最小值。 If the value of the element is less than this, the element fails constraint validation. 如果元素的值小于this,则元素将失败约束验证。 If a value is specified for min that isn't a valid number, the input has no minimum value. 如果为min指定的值不是有效数字,则输入没有最小值。

This value must be less than or equal to the value of the max attribute. 该值必须小于或等于max属性的值。

But in real seems like the min attribute also change basic value of my input,in total that increment value by user input wrong. 但实际上似乎min属性也改变了我输入的基本值,总的来说用户输入的增量值是错误的。

You can try this: 你可以试试这个:

Works fine(with no min attribute): https://jsfiddle.net/utqLrveh/7/ 工作正常(没有min属性): https//jsfiddle.net/utqLrveh/7/

Works wrong(with min attribute): https://jsfiddle.net/utqLrveh/6/ 工作错误(使用min属性): https//jsfiddle.net/utqLrveh/6/

Javascript part Javascript部分

var e=document.getElementById('basic');
e.setAttribute('value',16);
e.step=32;
e.max=59;

HTML part(works wrong with min attribute) HTML部分(min属性错误)

<input id="basic" type="number" value="0" min="5">

(works fine with no min attribute) (没有min属性,工作正常)

<input id="basic" type="number" value="0">

With no min the first user increment return 48(value=16+step=32); 没有min,第一个用户增量返回48(值= 16 +步长= 32); With min the first user increment return 37( ̶v̶a̶l̶u̶e̶=̶1̶6̶ min=5+step=32); 使用min,第一个用户增量返回37(̶v̶a̶l̶u̶e̶=̶1̶6̶min= 5 + step = 32);

The reason for this behaviour (in my understanding) is that once you set the min value (eg 5 ), the future values (that will be obtained after the <input> was incremented/decremented) would be based on it regardless of the default value . 这种行为(在我的理解中)的原因是,一旦你设置了min (例如5 ),未来的值(将在<input>递增/递减之后获得)将基于它而不管默认值如何value

In other words, if you set the value to say 16 and that the range is [5, 59] with a step of 32, the <input> can only get values such that any values val that you can obtain in the <input> field, you'll have this: val = input.min + n * input.step where n is the number of increments from the input.min (ie the minimum it can take). 换句话说,如果将value设置为16并且范围为[5,59]且步长为32,则<input>只能获取值,以便您可以在<input>获取任何值val现场,你就会有这样的: val = input.min + n * input.step其中n是从增量数input.min (即它可以取最小值)。

So in your case, you'll end up with having a range of values as follows [5, 37] (since 69 = 5 + 3 * 32 is above 59 and so are any 5 + 32 * n where n >= 3 ). 所以在你的情况下,你最终得到的值范围如下[5, 37] (因为69 = 5 + 3 * 32高于59 ,因此任何5 + 32 * n ,其中n >= 3 ) 。

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

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