jQuery:在选项类型编号中设置最小最大输入 [英] jquery: set min max input in option type number
本文介绍了jQuery:在选项类型编号中设置最小最大输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这部分代码
<input type="number" min="2" max="10" step="2" id="contact" oninput="new_sum">
在该字段中,我可以输入> 10且< 2.
In the field I can insert a number > 10 and < 2.
如何限制它?
推荐答案
添加onchange
函数并设置该值(如果该值超出范围).
add an onchange
function and set the value if it's out of the range.
$(function () {
$( "#numberBox" ).change(function() {
var max = parseInt($(this).attr('max'));
var min = parseInt($(this).attr('min'));
if ($(this).val() > max)
{
$(this).val(max);
}
else if ($(this).val() < min)
{
$(this).val(min);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="numberBox" type="number" min="2" max="10" step="2" id="contact" />
这篇关于jQuery:在选项类型编号中设置最小最大输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文