JQuery $(this).text().length 仅适用于 IE,不适用于 Chrome、IE 或 FF
JQuery $(this).text().length works only in IE and not on Chrome, IE or FF
我正在为带有 jquery 的文本区域创建一个简单的字符计数器,以限制用户可以提交的字符数。我已经设置了一个 keyup
事件处理程序来捕获文本更改事件。在 IE 上,$(this).text().length 完美。该事件被触发,当我检查文本区域的长度时,我得到了一个正确的数字。但是,同样的事情在 FF/Chrome 中不起作用。 text().length 始终 return 零(注意图片中的 cnt = 0;),即使文本区域中有字符。附上代码和提琴手图像。有人可以看到我做错了什么,使它在 IE 中工作,但在其他顶级浏览器中不工作吗?提前致谢。
//for replying the post by viewers. Limiting the number chars
$(document).on('keyup', '.txtareareply', function (b) {
var cnt = $(this).text().length;
var id = $(this).data("id");
$("#charMaxCountReply" + id).html(cnt + " of 1000 Character Max.")
if (cnt > 1000) {
alert("You have reached the maximum number of characters allowed.");
$(this).html($(this).text().substring(0, 1000));
return;
}
});
不使用 .text().length
,而是使用 .val().length
。这适用于多个控件,并将解决您的问题。
我正在为带有 jquery 的文本区域创建一个简单的字符计数器,以限制用户可以提交的字符数。我已经设置了一个 keyup
事件处理程序来捕获文本更改事件。在 IE 上,$(this).text().length 完美。该事件被触发,当我检查文本区域的长度时,我得到了一个正确的数字。但是,同样的事情在 FF/Chrome 中不起作用。 text().length 始终 return 零(注意图片中的 cnt = 0;),即使文本区域中有字符。附上代码和提琴手图像。有人可以看到我做错了什么,使它在 IE 中工作,但在其他顶级浏览器中不工作吗?提前致谢。
//for replying the post by viewers. Limiting the number chars
$(document).on('keyup', '.txtareareply', function (b) {
var cnt = $(this).text().length;
var id = $(this).data("id");
$("#charMaxCountReply" + id).html(cnt + " of 1000 Character Max.")
if (cnt > 1000) {
alert("You have reached the maximum number of characters allowed.");
$(this).html($(this).text().substring(0, 1000));
return;
}
});
不使用 .text().length
,而是使用 .val().length
。这适用于多个控件,并将解决您的问题。