手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >基于jquery的设置页面文本框 只能输入数字的实现代码
基于jquery的设置页面文本框 只能输入数字的实现代码
摘要:代码如下:复制代码代码如下:$("#money").bind("propertychange",function(){if(""!=this...

代码如下:

复制代码 代码如下:

$("#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;});

}

【基于jquery的设置页面文本框 只能输入数字的实现代码】相关文章:

jQuery结合ajax实现动态加载文本内容

JS动画效果打开、关闭层的实现方法

jquery实现点击label的同时触发文本框点击事件的方法

jQuery实现将页面上HTML标签换成另外标签的方法

显示行号的文本输入框

Jquery使用css方法改变样式实例

设为首页 加入收藏的js代码

解决未知尺寸的图片撑破页面的问题

jQuery实现给页面换肤的方法

一些有关检查数据的JS代码

精品推荐
分类导航