jQuery限制文本框輸入,包含粘貼。
//限制文本框的輸入長度$(function () { $(document).on("keypress", ".txt-valid-len", function (e) { if (e.keyCode == 8) { return true; } else { var len = $(this).data("maxlength") || 0; if (len > 0) { return (this.value.length <= len); } } return true; }); //粘貼 $(document).on("paste", ".txt-valid-len", function () { var len = $(this).data("maxlength") || 0; if (len > 0) { return ((this.value.length + event.clipboardData.getData('Text').length) <= len); } return true; }); $(document).on("keyup input", ".txt-valid-len", function (e) { var keyCode = e.keyCode || e.which || e.charCode; if (keyCode == 46 || keyCode == 8) { } else { var len = $(this).data("maxlength") || 0; if (len > 0) { if (this.value.length > len) { this.value = com.cutStr(this.value, len, ""); } } } });});以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持武林網(wǎng)!
新聞熱點
疑難解答