jQuery实现限制文本框的输入长度
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, ""); } } } }); });
相关推荐
chang00 2015-06-17
89453862 2013-07-10
王练 2020-06-12
81463166 2020-05-16
Reiki 2020-05-04
82550495 2020-05-01
zcl 2020-03-04
donghongbz 2020-01-17
tangfenjuan 2019-12-02
heshanking 2019-10-29
Passerby 2019-10-28
89463661 2019-11-05
Clarence 2011-11-18
xuewenke 2012-05-19
87281248 2018-03-13
zZoOoOJaVa 2018-02-19
mianhuantang 2009-12-11
srchange 2019-08-19
echoright晓戈 2011-11-21