文本区域计数 +最大长度
我正在尝试创建一个小的 JavaScript Textcount,其中将包含 Maxarea。
这是我的 JS:
function maxlength(item, max){
var a = $('#'+item+'').val();
var q = eval(""+a+".length");
var l = q - max
var msg = "Sorry but the max is "+max+", You have entered "+q+" characters into the textarea. Please delete at least "+l+" characters."
if (q > max){
$('#limit').html(msg);
}
}
这是 HTML:
<textarea id="area" onkeyup="maxlength('area', 12)"></textarea>
<br><br>
<div id="limit"></div>
问题是限制没有显示。
I'm trying to creating a little JavaScript Textcount which will include Maxarea.
Here is my JS:
function maxlength(item, max){
var a = $('#'+item+'').val();
var q = eval(""+a+".length");
var l = q - max
var msg = "Sorry but the max is "+max+", You have entered "+q+" characters into the textarea. Please delete at least "+l+" characters."
if (q > max){
$('#limit').html(msg);
}
}
With that this is the HTML:
<textarea id="area" onkeyup="maxlength('area', 12)"></textarea>
<br><br>
<div id="limit"></div>
The Problem is that the limit is not showing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要丢失 eval,
var q = eval(""+a+".length");
,并用此替换:a 已经是一个具有
length
的字符串财产。示例
You need to lose the eval,
var q = eval(""+a+".length");
, and replace with this:a is already a string with a
length
property.Example
你应该看看这个 jQuery 插件: http://unwrongest.com/projects/limit/
我发现它非常有用且易于实施。
You should have a look at this jQuery Plugin : http://unwrongest.com/projects/limit/
I've found it to be very useful and easy to implement.