How to set jQuery spinner max and min values?

I ran into the following problem: I am using jQuery + ajax and I want my max and min spinner values ​​to be set after the data has been received from sever. But the real situation is that when I check the element with firebug, it says that the max and min values ​​are set, but in fact they are not, so there are no max and min values. here is the code:

... <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 configured via ajax. I also noticed that the value is set correctly, i.e. $('#spinId').attr('value',ValueFromServer); works fine. so what should i do to fix this?

+4
source share
2 answers

The min and max values ​​are set when creating the counter. To change them on the fly use this:

 // 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() function can only be used to get and set the attributes of DOM elements. For example, you can use it to get or set the id of the <div> element or href of the <a> element. Most jQuery plugins save their configuration in JavaScript variables. If they attach the configuration to the DOM, they use .data() .

+4
source

You can also set parameters directly on different players without affecting the default values. Here's a working example: 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, the jQuery spinner has some problems with meeting the minimum / maximum values ​​when clicking the buttons to the right of the input. The value may exceed the min / max value, but will be checked after blurring the user from the field. Checking the correct operation when using the up / down arrow keys, so it seems that for the author of the plugin there is room for improvement.

+4
source

All Articles