我在这里使用onPaste
事件: -
$(document).on("paste", ".txtQuestion", function (event e)
if ( $(".txtQuestion").val().length >= 10) {
e.preventDefault();
$(".txtQuestion").addClass("error");
return false;
}
else {
$(".txtQuestion.error").removeClass("error");
}
});
答案 0 :(得分:1)
你可以看看这个:
$(document).on("paste keydown", ".txtQuestion", function (e) {
var $this = $(this); //chache your element
var max = 10; //set max-value for input
setTimeout(function () { //workaround for catching the pasted input
var text = $this.val(); //get the value of the textarea
if (text.length >= max) { //check for the length
e.preventDefault();
$this.addClass("error");
$this.val(text.substring(0, max)); //cut the input to the max
} else {
$this.removeClass("error");
}
}, 100);
});
有关捕获粘贴输入的setTimeout
的解决方法,请参阅here
答案 1 :(得分:0)
仅仅缩短长度限制是不是更好?所以在改变时你会检查长度和substr(0,限制)?