0

我正在使用这个脚本来创建一个只有数字的文本字段,它工作得很好,只有我的用户不能超过一个数字,而且我需要 7 位以上的数字,我猜很多人会使用复制粘贴。

我看到了一些使用 jquery 的解决方案,但接缝对我来说有点太重了,并且必须有一些允许粘贴的轻巧优雅的解决方案?

4

1 回答 1

0

You can use the number type introduced in HTML5:

<input type="number" min="0" max="9999999">

It’s not supported everywhere, but you can add a fallback for IE using the input.onpropertychange event and check for window.event.propertyName == 'value' inside the handler to listen for any changes in the input field.

For other browsers, you can try the "input" event. Or simply validate and modify on blur to be nicer to the user.

As others have mentioned, it’s considered bad practice to do this as the user types. The number attribute adds a pretty decent way of handling this on it’s own.

于 2011-10-02T17:40:28.253 回答