zoukankan      html  css  js  c++  java
  • jQuery控制文本框只能输入数字[兼容IE、火狐等浏览器]

    $.fn.numeral=function(bl){//限制金额输入、兼容浏览器、屏蔽粘贴拖拽等
    	  $(this).keypress(function(e){
    		  var keyCode=e.keyCode?e.keyCode:e.which;
    		if(bl){//浮点数
    		  if((this.value.length==0 || this.value.indexOf(".")!=-1) && keyCode==46) return false;
    		  return keyCode>=48&&keyCode<=57||keyCode==46||keyCode==8;
    		}else{//整数
    		  return  keyCode>=48&&keyCode<=57||keyCode==8;
    		}
    	  });
    	  $(this).bind("copy cut paste", function (e) { // 通过空格连续添加复制、剪切、粘贴事件 
    		  if (window.clipboardData)//clipboardData.setData('text', clipboardData.getData('text').replace(/D/g, ''));
    			  return !clipboardData.getData('text').match(/D/);
    		  else 
    			  event.preventDefault();
    	   }); 
    	  $(this).bind("dragenter",function(){return false;});
    	  $(this).css("ime-mode","disabled");
    	  $(this).bind("focus", function() {   
            if (this.value.lastIndexOf(".") == (this.value.length - 1)) {   
                this.value = this.value.substr(0, this.value.length - 1);
            } else if (isNaN(this.value)) {   
                this.value = "";   
            }   
        });   
    }
    

    使用方法:

    $("#num").numeral(false);//限制只能输入整数
    $("#price").numeral(true);//限制只能输入浮点数
    

      

  • 相关阅读:
    codevs 3657 括号序列
    洛谷P1962 斐波那契数列
    Black Rock shooter
    codevs 2596 售货员的难题
    51Nod-1154 回文串划分
    UVA
    POJ3321[苹果树] 树状数组/线段树 + dfs序
    Hdu 4578 Transformation (线段树 分类分析)
    786B
    438D
  • 原文地址:https://www.cnblogs.com/briny/p/3488997.html
Copyright © 2011-2022 走看看