作家
登录

基于jquery的设置页面文本框 只能输入数字的实现代码

作者: 来源:www.28hudong.com 2013-03-30 00:54:07 阅读 我要评论

代码如下: 复制代码 代码如下: $("#money").bind("propertychange",function() { if(""!=this.value){ var str = this.value.replace(/(^s*)|(s*$)/g, ""); if(this.value != str ) this.value = str; } if( isNaN(Number(this.value))) this.value = this.value.replace(/[D]/,''); }); 这里使用了JQuery绑定到id为money的文本框的onpropertychange事件上。 下面的代码连小数点也屏蔽掉了 复制代码 代码如下: $("#phone").bind("propertychange", function() { if(""!=this.value){ var str = this.value.replace(/(^s*)|(s*$)/g, ""); if(this.value != str ) this.value = str; } if (this.value.indexOf('.') != -1) { this.value = this.value.replace(/[.]/, ''); this.focus(); } if (isNaN(Number(this.value))) { this.value = ($.trim(this.value)).replace(/[D]/, ''); this.focus(); } }); 最后,最好将输入法屏蔽掉。 通过css,ime-mode:disabled就可以实现。 如果很严格的话,可以再追加上禁止粘贴与拖拽。 禁止粘贴与拖拽实现方法文本框禁止拖拽和粘贴 在css中实现文本框禁止拖拽和粘贴的功能 建立一个Css,如下: 复制代码 代码如下: .TextBox_NotDragpaste { ondragenter:expression(ondragenter=function(){return false;}); onpaste:expression(onpaste=function(){return false;}); } 如果还需要禁止输入中文的功能只需要多加一个语句即可。 如下: 复制代码 代码如下: .TextBox_NotDragpaste { ime-mode:disabled; ondragenter:expression(ondragenter=function(){return false;}); onpaste:expression(onpaste=function(){return false;}); }

  推荐阅读

  JavaScript之HTMLCollection接口代码

复制代码 代码如下: interface HTMLCollection { //包含结点的个数 readonly attribute unsigned long length; //根据指定的索引index,返回相应的结点 //HTMLCollection中的结点呈树形结构,索引值index是结点深度>>>详细阅读


本文标题:基于jquery的设置页面文本框 只能输入数字的实现代码

地址:http://www.17bianji.com/kaifa2/JS/24644.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)