[英]Javascript for loop not working on function keydown
In my code for alert all the lines of textarea filed on every keydown event but loop not working 在我的警报代码中,每个keydown事件都记录了textarea的所有行,但循环不起作用
function limitTextareaLine(e) {
var textArray = $(this).val().split("\n");
for(var v in textArray){ // Only iterate first line
alert(textArray[v] +" "+textArray.length);
}
}
$(function() {
$('textarea.limited').keydown(limitTextareaLine);
});
In Javascript you need to loop through the array using numbers, you were using the values in the array. 在Javascript中,您需要使用数字来遍历数组,而您正在使用数组中的值。 You need the following: 您需要以下内容:
function limitTextareaLine(e) {
var textArray = $(this).val().split("\n");
for(v=0; v < textArray.length; v++){
alert(textArray[v] +" "+textArray.length);
}
}
$(function() {
$('textarea.limited').keyup(limitTextareaLine);
});
I've also changed the event to keyup so that it triggers the event after the key has been recorded in the box. 我还将事件更改为keyup,以便在将密钥记录在框中之后触发事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.