我有这部分代码
<input type="number" min="2" max="10" step="2" id="contact" oninput="new_sum">
在该字段中,我可以插入一个大于 10 且小于 2 的数字。
我怎样才能限制它?
add an onchange
函数并设置值(如果超出范围)。
$(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" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)