Javascript 如何设置 jQuery 微调器的最大值和最小值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11843219/
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-24 07:19:52  来源:igfitidea点击:

How to set jQuery spinner max and min values?

javascriptjqueryajaxspinner

提问by Helgus

i've faced the next problem: I'm using jQuery + ajax and I want my spinner's max and min values to be set after the data is obtained from the sever. But the real situation is that when I check the element with firebug, it says, that max and min values are set and in reality they aren't, so there is no max and min values. here is some code:

我遇到了下一个问题:我正在使用 jQuery + ajax,我希望在从服务器获取数据后设置微调器的最大值和最小值。但实际情况是,当我用萤火虫检查元素时,它说设置了最大值和最小值,而实际上它们没有设置,因此没有最大值和最小值。这是一些代码:

...
<input type="number" id ="spinId" name="someName" step="0.5" />
...
$("#spinId").attr('min', 0.0);
$("#spinId").attr('max', parseFloat($('.someClass').text()));
...

and this .someClass is formed via ajax. also I noticed that value is set properly, i.e. $('#spinId').attr('value',ValueFromServer);works fine. so what should I do to fix it?

这个 .someClass 是通过 ajax 形成的。我还注意到值设置正确,即$('#spinId').attr('value',ValueFromServer);工作正常。那么我该怎么做才能解决它?

采纳答案by Peter-Paul van Gemerden

The minand maxvalue are set when the spinner is created. To change them on the fly, use this:

minmax创建微调当值设置。要即时更改它们,请使用以下命令:

// given a 'data' object from AJAX with keys 'min' and 'max':
$('#spinId').spinner('option', 'min', data.min);
$('#spinId').spinner('option', 'max', data.max);

Also keep in mind that the .attr()functioncan only be used to get and set attributes of DOM elements. For example, you can use it to get or set the idof a <div>or the hrefof an <a>element. Most jQuery plugins keep their configuration in JavaScript variables. If they do attach configuration to the DOM, they use .data().

还要记住,该.attr()函数只能用于获取和设置 DOM 元素的属性。例如,你可以用它来获取或设置id的一个<div>href一个的<a>元素。大多数 jQuery 插件将它们的配置保存在 JavaScript 变量中。如果他们确实将配置附加到 DOM,他们会使用.data().

回答by WTK

You can also set options directly on different spinners without affecting the defaults. Here's working example: http://jsfiddle.net/7xj7K/4/

您还可以直接在不同的微调器上设置选项,而不会影响默认值。这是工作示例:http: //jsfiddle.net/7xj7K/4/

// give two different inputs different options without changing jquery spinner defaults
$("#spinId").spinner({min: 0, max: 10});
$("#spinId_two").spinner({min: -3, max: 53});

As you can see on jsfiddle, jQuery spinner has some issues with enforcing min/max values while clicking buttons to the right of the input. The value can than exceed min/max value but will be validated once user blur from the field. Validation works just fine when using keyboard up/down arrows so it seems like there's room for improvement for author of the plugin.

正如您在 jsfiddle 上看到的那样,jQuery spinner 在单击输入右侧的按钮时在强制执行最小值/最大值方面存在一些问题。该值可以超过最小/最大值,但一旦用户从该字段中模糊,就会被验证。使用键盘向上/向下箭头时,验证工作得很好,因此插件作者似乎还有改进的余地。