jQuery.val 不适用于范围输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10921368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:57:51  来源:igfitidea点击:

jQuery.val not working on range input?

jqueryinputrange

提问by Alex

<input type="range" value="5,17" />

<input type="range" value="5,17" />

If I try to change the value with $('input').val('8,20');it doesn't change...

如果我尝试更改$('input').val('8,20');它的值不会改变......

But on hidden inputs works: <input type="hidden" value="s" />

但在隐藏输入上有效: <input type="hidden" value="s" />

回答by Seybsen

The HTML5 <input type="range" />does only handle one value between minand maxattribute - e.g.:

HTML5<input type="range" />只处理minmax属性之间的一个值- 例如:

<input type="range" min="1" max="10" value="5.3" step="0.1" />

$("input[type=range]").val(); // returns 5.3
$("input[type=range]").val(6); // sets value to 6
$("input[type=range]").val(); // returns 6

If you want to have the slide handle a min anda max value in one UI-input you could possibly use jQueryUI's slider: https://jqueryui.com/slider/#range

如果你想让幻灯片在一个 UI 输入中处理一个最小值一个最大值,你可以使用 jQueryUI 的滑块:https://jqueryui.com/slider/#range

回答by Alessandro Vendruscolo

I tried setting up a fiddle and it worked without problems.

我尝试设置一个小提琴并且它没有问题。

http://jsfiddle.net/Z2B7Y/

http://jsfiddle.net/Z2B7Y/

Keep in mind that the valuerepresents the numerical value, that's between the minand maxattributes. Trying to set it to 8,20it's not valid, because it's not a valid number and therefore doesn't make sense.

请记住,value代表数值,位于minmax属性之间。试图将其设置为8,20无效,因为它不是有效数字,因此没有意义。

If you want to set a floating point value you have to use the dot, e.g.

如果你想设置一个浮点值,你必须使用点,例如

$('input').val('8.20');

If you want to set instead the bounds of the range you have to change the properties, e.g.

如果您想设置范围的边界,则必须更改属性,例如

$('input').prop('min','8');
$('input').prop('max','20');

Hope this helps.

希望这可以帮助。