Textarea count + Max Length
I'm trying to creating a little JavaScript Textcount which wi开发者_Go百科ll 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.
You need to lose the eval, var q = eval(""+a+".length");
, and replace with this:
var q = a.length;
a is already a string with a length
property.
Example
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.
精彩评论