如何使用Jquery限制数字输入框中的某些数字范围 [英] How to restrict certain range of numbers in numeric input box using Jquery
本文介绍了如何使用Jquery限制数字输入框中的某些数字范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何限制数字框中的所需范围?
解决方案
尝试使用以下代码:
// 仅允许数字
(' #inputTxt')。keydown( function ( e){
if (e.shiftKey || e.ctrlKey || e.altKey){
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8 )||(key == 46 )||(键> = 35 &&& = 40 )||(键> = 48 && key< = 57 ) ||( key> = 96 && key< = 105 ))){
e.preventDefault();
}
}
});
// 允许值介于50到100之间 >
( #inputTxt)。on( 更改, function (){
< span class =code-keyword> var val = parseInt ( this .value) ;
if (val> 100 || val< 50 )
{
alert(' 错误的范围');
此 .value = ' ';
}
})
这里inputTxt是文本框的ID。您可以根据需要进行更改。
How to restrict the desired range in the numeric box?
解决方案Try with below code:
// To allow only numbers
('#inputTxt').keydown(function (e) { if (e.shiftKey || e.ctrlKey || e.altKey) { e.preventDefault(); } else { var key = e.keyCode; if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) { e.preventDefault(); } } }); // To allow value within range between 50 to 100
("#inputTxt").on("change", function() { var val = parseInt(this.value); if(val > 100 || val < 50) { alert('Wrong range'); this.value =''; } })
Here inputTxt is ID of text box. You can change as per your need.
这篇关于如何使用Jquery限制数字输入框中的某些数字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文