jquery限定文本框只能輸入數字即整數和小數
更新時間:2013年11月29日 17:53:45 作者:
限定文本框只能輸入數字即整數和小數,在某些特殊情況下還是比較實用的,下面有個不錯的示例,通過jquery來簡單實現(xiàn)下
復制代碼 代碼如下:
$(function(){
//文本框只能輸入數字(不包括小數),并屏蔽輸入法和粘貼
$.fn.integer= function() {
$(this).css("ime-mode", "disabled");
this.bind("keypress",function(e) {
var code = (e.keyCode ? e.keyCode : e.which); //兼容火狐 IE
if(!$.browser.msie&&(e.keyCode==0x8)){ //火狐下不能使用退格鍵
return ;
}
return code >= 48 && code<= 57;
});
this.bind("paste", function() {
return false;
});
this.bind("keyup", function() {
if (/(^0+)/.test(this.value)) {
this.value = this.value.replace(/^0*/, '');
}
});
};
//文本框只能輸入數字(包括小數),并屏蔽輸入法和粘貼
$.fn.number= function() {
$(this).css("ime-mode", "disabled");
this.bind("keypress",function(e) {
var code = (e.keyCode ? e.keyCode : e.which); //兼容火狐 IE
if(!$.browser.msie&&(e.keyCode==0x8)){ //火狐下不能使用退格鍵
return ;
}
if(this.value.indexOf(".")==-1){
return (code >= 48 && code<= 57)||(code==46);
}else{
return code >= 48 && code<= 57
}
});
this.bind("paste", function() {
return false;
});
this.bind("keyup", function() {
if(this.value.slice(0,1) == "."){
this.value = "";
}
});
this.bind("blur",function(){
if(this.value.slice(-1) == "."){
this.value = this.value.slice(0,this.value.length-1);
}
});
};
});
相關文章
EasyUI的treegrid組件動態(tài)加載數據問題的解決辦法
最近涉及到treegrid組件的查詢,需要根據查詢條件動態(tài)更新EasyUI的treegrid組件的動態(tài)加載查詢結果2011-12-12
使用JQuery和CSS模擬超鏈接的用戶單擊事件的實現(xiàn)代碼
使用JQuery和CSS模擬超鏈接的用戶單擊事件的實現(xiàn)代碼,需要的朋友可以參考下2012-05-05

