有时我们需要我们的input只能输入整数,把中文,英文以及所有字符全部屏蔽掉,我找到了一个非常简便的方法,分享给大家,如果大家有更好的也请一起分享哈!
只需要在input中加入以下代码即可:
style="ime-mode:disabled" onkeypress="return event.keyCode>=48&&event.keyCode<=57" onpaste="return !clipboardData.getData('text').match(/\D/)" ondragenter="return false"
完整的input案例代码如下:
<
input
type
=
"text"
name
=
"exchangeamount"
id
=
"exchangeamount"
value
=
"0"
size
=
"5"
style
=
"ime-mode:disabled"
onkeypress="return event.keyCode>=48&&event.keyCode<=57" onpaste="return !clipboardData.getData('text').match(/\D/)" ondragenter="return false" />
发现楼上的方法在ff浏览器下导致无法输入
又找了一个新的,在ff和ie下测试通过
完整input案例代码如下:
1
<
input
type
=
"text"
name
=
"exchangeamount"
id
=
"exchangeamount"
value
=
"0"
size
=
"5"
style
=
"ime-mode:disabled"
onKeyUp
=
"this.value=this.value.replace(/[^\.\d]/g,'');this.value=this.value.replace('.','');"
/>