只能输入数字装X版
jquery.onlyNum.js:
(function ($) { $.fn.numeric = function (only_integer, precision) { var decimal = only_integer === false ? "." : ""; var reg = new RegExp("[^0-9" + decimal + "]", "g"); var precision = precision || 2; this.css("ime-mode", "disabled"); this.keyup(function (e) { var key = e.which; if (key != 8 && key != 9 && key != 13 && key != 16 && key != 35 && key != 36 && key != 37 && key != 39 && key != 46) { var position = $(this).cursorPosition(); var old_len = $(this).val().length; $(this).val($(this).val().replace(reg, "")); $(this).cursorPosition(position - (old_len - $(this).val().length)) } }); this.keydown(function(e) { var key = e.which; if (only_integer === false && key === 190) { if (this.value.indexOf(".") > -1) { return false } } }); this.blur(function () { if ($(this).hasClass("placeholder")) { return } if ($(this).val().length > 1) { var tmp = parseFloat($(this).val()); tmp = isNaN(tmp) ? "" : tmp + ""; if (only_integer === false) { var index = tmp.indexOf("."); if (index > -1) { tmp = tmp.substring(0, index + precision + 1) } } $(this).val(tmp) } }); this.bind("dragover", function (e) { e.originalEvent.dataTransfer.dropEffect = "none"; e.preventDefault(); return false }); return this }; $.fn.cursorPosition = function (value) { var elem = this[0]; if (elem && (elem.tagName == "TEXTAREA" || elem.type.toLowerCase() == "text")) { if (window.attachEvent) { var rng; if (elem.tagName == "TEXTAREA") { rng = event.srcElement.createTextRange(); rng.moveToPoint(event.x, event.y) } else { rng = document.selection.createRange() } if (value === undefined) { rng.moveStart("character", -event.srcElement.value.length); return rng.text.length } else { if (typeof value === "number") { var index = this.cursorPosition(); index > value ? (rng.moveEnd("character", value - index)) : (rng.moveStart("character", value - index)); rng.select() } } } else { if (value === undefined) { return elem.selectionStart } else { if (typeof value === "number") { elem.selectionEnd = value; elem.selectionStart = value } } } } } })(jQuery);