有没有办法将范围输入的默认值设置为空? [英] Is there a way to set default value of range input to be empty?

查看:84
本文介绍了有没有办法将范围输入的默认值设置为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML5范围输入

I have a HTML5 range input

<input name="testing" type="range" min="0" max="10" step="1">

当未设置属性值"时,它将默认值设置为中间值(在这种情况下为5).有没有一种方法可以使它为空,以便让我知道用户是否已在主动输入内容?

When there is no attribute "value" set, it sets the default value to the middle (i.e. 5 in this case). Is there a way to make it empty so that I know if the user has actively input something?

我还有另一个用于显示值的div.开头的值将是?",因此用户将知道他们没有输入任何内容.问题在于,如果未主动设置值,则阻止输入将值提交给后端.

I have another div for value display. At the beginning the value will be "?", so the user will know they haven't input anything. The problem is to prevent the input from submitting value to backend if value is not actively set.

推荐答案

不幸的是,这是对[type=range]元素的限制:

Unfortunately this is a restraint of the [type=range] element:

默认值为最小值加上最大值与最小值之间的差值的一半,除非最大值小于最小值,在这种情况下,默认值为最小值.

The default value is the minimum plus half the difference between the minimum and the maximum, unless the maximum is less than the minimum, in which case the default value is the minimum.

HTML5规范

如果除了使用该元素别无选择,建议您在范围内添加一个额外的值,然后在验证表单时检查该值. 即如果您当前的范围是0到10,则将范围设置为-1到10,然后将值默认为-1;否则,默认值为-1.在验证方面,检查该值是否不等于-1.

If you have no alternative other than to use this element, I'd suggest adding an extra value to your range, then checking for that value when validating the form. I.e. if your current range is 0 to 10, make your range -1 to 10, then default the value to -1; on the validation side, check if the value is not equal to -1.

这篇关于有没有办法将范围输入的默认值设置为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆