jQuery 如何通过javascript或jquery在html5中设置<input>的最大值和最小值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19316709/
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
How to set the max value and min value of <input> in html5 by javascript or jquery?
提问by user2625363
I am trying to find out how to set the max value and min value of html5 type input by javascript or jquery.
我试图找出如何设置由 javascript 或 jquery 输入的 html5 类型的最大值和最小值。
<input type="number" max="???" min="???" step="0.5"/>
Would someone please guide
有人请指导
回答by nnnnnn
jQuery makes it easy to set any attributes for an element - just use the .attr()
method:
jQuery 可以轻松地为元素设置任何属性 - 只需使用以下.attr()
方法:
$(document).ready(function() {
$("input").attr({
"max" : 10, // substitute your own
"min" : 2 // values (or variables) here
});
});
The document ready handler is not required if your script block appears after the element(s) you want to manipulate.
如果您的脚本块出现在您要操作的元素之后,则不需要文档就绪处理程序。
Using a selector of "input"
will set the attributes for allinputs though, so really you should have some way to identify the input in question. If you gave it an id you could say:
使用选择器"input"
将设置所有输入的属性,所以你真的应该有一些方法来识别有问题的输入。如果你给它一个 id 你可以说:
$("#idHere").attr(...
...or with a class:
...或与一个类:
$(".classHere").attr(...
回答by Dhaval Bharadva
Try this:
尝试这个:
<input type="number" max="???" min="???" step="0.5" id="myInput"/>
$("#myInput").attr({
"max" : 10,
"min" : 2
});
Note:This will set max and min value only to single input
注意:这只会将最大值和最小值设置为单个输入