具有最小和最大范围的 jquery 数字文本框
我看到这个问题,但没有一个解决方案支持最小值和最大值范围
如何使用 jQuery 在 HTML 输入框中仅允许数字(0-9)?
我看到 http://jstepper.emkay.dk/ 尝试这样做,但似乎非常有问题,因为它允许您输入多个小数位和其他字符,例如“;”。
有没有一种方法可以使用这些解决方案之一,并且只支持文本框条目中的最小值 0 和最大值 100?
i see this question but none of the solutions support min and max values range
How to allow only numeric (0-9) in HTML inputbox using jQuery?
I see that http://jstepper.emkay.dk/ attempts to do this but seems very buggy as it allows you to enter multiple decimal places and other characters such as ";".
is there i way i can use one of these solutions and also say, only support min of 0 and max of 100 in the textbox entry?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我是 jStepper 的作者,我刚刚更新了该插件,以便能够做您喜欢的事情。
所以再试一次,看看现在是否有效:)
I am the author of jStepper and I just updated the plugin to be able to do what you like.
So give it a try again and see if it works now :)
添加此类字段的 HTML5 方式。适用于 Chrome 和 Safari:
完整示例:
HTML5 way of adding such fields. Works in Chrome and Safari:
A complete example:
创建仅接受范围的自定义类型的文本框。像这样的东西应该可以工作:
要使用它,只需通过调用 new RangeTextBox(min, max) 创建一个新的 TextBox 即可。例如
免责声明:这与 Harmen 在字段的其他答案中列出的问题相同,如果最小值为 6,则不接受数字“26”的 2 之类的值,因为 2 < 6. 您可以使用计时器,或者实际上将此检查移到
keyup
和keydown
事件之外,并检查onblur
或onchange
相反。虽然这会允许无效数据进入该字段,但不会那么烦人。编辑:此解决方案允许您输入任何内容,但会标记所有无效输入,包括超出范围的输入。
和样式表:
Create a custom type of text box that only accepts ranges. Something like this should work:
To use it in, simple create a new TextBox by calling
new RangeTextBox(min, max)
. For exampleDisclaimer: This suffers from the same problem as listed by Harmen in the other answer of field not accepting a value like 2 for the number "26" if minimum value is 6 as 2 < 6. You could use a timer, or actually move this check outside of the
keyup
andkeydown
events, and checkonblur
oronchange
instead. That would allow invalid data to enter the field though but its a lot less annonying.Edit: This solution let's you enter anything, but flags all invalid input including out of ranges.
And the stylesheet:
文本框 MIN MAX - jQuery 函数
例如,这里有一种使用 jQuery“在文本框条目中仅支持最小值 0 和最大值 100”的方法。它的工作原理是将您的输入返回到之前的状态 - 如果您输入的键导致值小于 MIN 或大于 MAX。您可以设置一个标志以仅接受整数。
HTML 标记如下所示: (JSFIDDLE)
然后您将在文档中运行此 JavaScript准备好的代码:
我对此很陌生,因此任何建设性的意见将不胜感激。
Textbox MIN MAX - jQuery function
Here is a way to for example, "only support min of 0 and max of 100 in a textbox entry" using jQuery. It works by returning your entry to what it was before - if you enter a key that results in the value being less than MIN or more that MAX. You can set a flag to accept only integers.
The HTML tag would look like this: (JSFIDDLE)
Then you'd run this JavaScript in your document ready code:
I'm new to this, so any constructive comments would be appreciated.
以下是您的脚本的示例:
但它有一个很大的缺点,如果最小值是 6,则不能输入 26,因为 2 < 6. 如果您的最小值小于或等于 10,这不是问题。
但是,如果您的最小值大于 10,您可以考虑以下代码:
它会延迟一秒检查输入。为此,您需要一些 CSS 代码,例如:
两个脚本都会动态检查值,这是一个很好的方法。
Here is an example of how your script could look like:
But it has a big disadvantage, you can't enter 26 if the minimum is 6, because 2 < 6. This is not a problem if you have a minimum less than or equal to 10.
However, if your minimum is more than 10, you could consider this code:
It checks the input with a delay of one second. You need some CSS code for this, for example:
Both scripts check the values on the fly, which is a great method.
我创建了一个支持最小值、最大值的 jQuery SpinControl和步骤。它首先检查浏览器 (Opera) 是否已支持 SpinControl,然后再添加自定义控件。
I've created an jQuery SpinControl that supports min, max and step. It first checks if a SpinControl already is supported by the browser (Opera), before it adds a custom one.
我进行了搜索,最后我明白输入的值必须在三个事件中进行检查。 keydown、keypress 和 keyup,它们中的任何一个都有自己的职责。
按键:
检查按下的键并确定输入的字符。然后,如果它是数字,则让它否则阻止该操作。正如你所看到的,我尝试在这里检查范围,
但在此之前我们只有新的字符和值。因此,如果我们尝试检查它们的总和,这是错误的方法,因为用户可能选择了值的某些部分,而新字符则相反。假设最大范围是 100,我们在输入中有“345”,用户选择“34”,现在用户按“2”,结果必须是“25”,但“345”+“2”的总和是 347,并且函数阻止了这种情况。所以我决定使用 keyup 事件来检查范围和正确的值。最后,当浏览器支持 HTML5 数字类型输入时, keydown 用于检查上下箭头和控制值,而不像 keypress 那样改变,因此您可以不使用它。
我写了一个简单的脚本,希望有所帮助。您可以在 http://jsfiddle.net/QMaster/r4hwr3a6/ 中看到它
I searched and finally i understand entered value must be check in three event. keydown, keypress and keyup and any of them has own responsibilities.
keypress:
Check the key pressed and decided about entered character. Then if it is numeric let it otherwise prevent the action. As you can see i tried to check range here
but we just have new character and value before that. so that is wrong way if we try to check sum of them because maybe user selected some part of the value and new character standing rather. suppose max range is 100 and we have "345" in Input and user selected "34" now user press "2" and the result must be "25", but sum of "345" + "2" is 347 and function prevent that. So i decided to use keyup event to check range and correct value. Finally keydown used to check up and down arrow and control value when browser support HTML5 number type of input without changing like keypress so you can don't use that.
I wrote a simple script and hope that help. You can see it in http://jsfiddle.net/QMaster/r4hwr3a6/
JStepper 有一个错误,
我在他们的网站上尝试了测试仪。
http://jstepper.emkay.dk/Default.aspx
应用规则,
但我可以输入文字:8778.09999
JStepper have a bug,
I try the tester in their site.
http://jstepper.emkay.dk/Default.aspx
apple the rules,
but i can input the text : 8778.09999