源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-11-07 03:18 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:基于jquery的设置页面文本框 只能输入数字的实现代码
代码如下:
[u]复制代码[/u] 代码如下:
$("#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事件上。 下面的代码连小数点也屏蔽掉了
[u]复制代码[/u] 代码如下:
$("#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就可以实现。 如果很严格的话,可以再追加上禁止粘贴与拖拽。 [b]禁止粘贴与拖拽实现方法 [/b]文本框禁止拖拽和粘贴 在css中实现文本框禁止拖拽和粘贴的功能 建立一个Css,如下:
[u]复制代码[/u] 代码如下:
.TextBox_NotDragpaste { ondragenter:expression(ondragenter=function(){return false;}); onpaste:expression(onpaste=function(){return false;}); }
如果还需要禁止输入中文的功能只需要多加一个语句即可。 如下:
[u]复制代码[/u] 代码如下:
.TextBox_NotDragpaste { ime-mode:disabled; ondragenter:expression(ondragenter=function(){return false;}); onpaste:expression(onpaste=function(){return false;}); }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部