我正在追踪输入textarea的字符数< 140.我目前只在'keyup'上捕捉字符,但也想跟踪用户是否复制/粘贴到textarea中。有什么建议吗?
$(function(){
var limitDiv = $("#limit");
var limit = limitDiv.data("limit");
var button = $("input");
$("textarea").on("keyup", function(){
var total = this.value.length;
var diff = limit- total;
var condition = diff < 0 || total === 0;
limitDiv.text(diff).toggleClass("red", condition);
button.prop("disabled", condition);
}).trigger("keyup");
})
答案 0 :(得分:1)
jQuery中有一个paste
事件,但它并不经常使用。它似乎仍然适用于jQuery 2.1.0.
$("#input").on("paste", function(e){
//code goes here
});
答案 1 :(得分:0)
只需添加粘贴事件:
$(function(){
var limitDiv = $("#limit");
var limit = limitDiv.data("limit");
var button = $("input");
$("textarea").on("keyup paste", function(){
var total = this.value.length;
var diff = limit- total;
var condition = diff < 0 || total === 0;
limitDiv.text(diff).toggleClass("red", condition);
button.prop("disabled", condition);
button.value(total);
});
})
&#13;
textarea {
width: 100%;
height: 30px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea></textarea>
<input type="button" value="Submit">
<div id="limit" data-limit="15"></div>
&#13;