如何设置< input>的最大值和最小值.在html5中是通过javascript还是jquery? [英] How to set the max value and min value of <input> in html5 by javascript or jquery?

查看:419
本文介绍了如何设置< input>的最大值和最小值.在html5中是通过javascript还是jquery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何设置通过javascript或jquery输入的html5类型的最大值和最小值.

I am trying to find out how to set the max value and min value of html5 type input by javascript or jquery.

<input type="number" max="???" min="???" step="0.5"/>

请有人指导

推荐答案

jQuery使设置元素的任何属性变得容易-只需使用 .attr()方法:

jQuery makes it easy to set any attributes for an element - just use the .attr() method:

$(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.

使用"input"的选择器将设置 all 输入的属性,因此,实际上,您应该有一些方法可以标识出有问题的输入.如果您给它提供一个ID,则可以说:

Using a selector of "input" will set the attributes for all inputs though, so really you should have some way to identify the input in question. If you gave it an id you could say:

$("#idHere").attr(...

...或带有课程:

$(".classHere").attr(...

这篇关于如何设置&lt; input&gt;的最大值和最小值.在html5中是通过javascript还是jquery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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