将输入框限制为0-100 [英] Limit input box to 0-100
问题描述
我有一个输入框,取值范围为0-100。我想阻止用户写更大或更小的东西。
I have an input box that takes values from 0-100. I want to prevent users from writing anything larger or smaller.
我一直在使用jquery keyfilter插件来限制字段的输入:
http://code.google.com/p/jquery-keyfilter/
I've been using the jquery keyfilter plugin to limit the input of a field: http://code.google.com/p/jquery-keyfilter/
此插件可以将输入限制为数字,但不在一定范围内。如何阻止用户输入超出范围的数字。谢谢。
This plugin can limit the input to numerals, but not within a range. How do I prevent users from entering numbers that would exceed the range. Thanks.
推荐答案
使用像这样的普通Javascript:
Use plain Javascript like this:
<script>
function handleChange(input) {
if (input.value < 0) input.value = 0;
if (input.value > 100) input.value = 100;
}
</script>
然后如下所示声明输入框:
Then declare the input box like this:
<input type="text" onchange="handleChange(this);" />
因为您已将此函数挂钩到onchange(),即使用户复制/粘贴它也能正常工作输入框中的内容。
Because you have hooked this function to onchange(), it will work even if a user copy / pastes something into the input box.
这篇关于将输入框限制为0-100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!