将输入框限制为 0-100 [英] Limit input box to 0-100

查看:24
本文介绍了将输入框限制为 0-100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入框,它的值是 0-100.我想阻止用户编写更大或更小的任何内容.

我一直在使用 jquery keyfilter 插件来限制字段的输入:http://code.google.com/p/jquery-keyfilter/>

这个插件可以将输入限制为数字,但不能在一个范围内.如何防止用户输入超出范围的数字.谢谢.

解决方案

像这样使用普通的 Javascript:

然后像这样声明输入框:

因为您已将此函数挂接到 onchange(),因此即使用户将某些内容复制/粘贴到输入框中,它也会起作用.

I have an input box that takes values from 0-100. I want to prevent users from writing anything larger or smaller.

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.

解决方案

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);" />

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屋!

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