摘要:代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>jquery限制输入字数,并提示剩余字数</title> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> function words_deal() { var curLength=$("#TextArea1").val().length; if(curLength>5) { var num=$("#TextArea1").val().substr(0,5); $("#TextArea1").val(num); alert("超过字数限制,多出的字将被截断!" ); } else { $("#textCount").text(5-$("#TextArea1").val().length); } } </script> </head> <body> 剩余<span id="textCount">5</span>个字<br /> <textarea name="textarea" id="TextArea1" cols="45" rows="5" onkeyup="words_deal();" ></textarea> </body> </html>

小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery限制输入字数,并提示剩余字数</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function words_deal()
{
var curLength=$("#TextArea1").val().length;
if(curLength>5)
{
var num=$("#TextArea1").val().substr(0,5);
$("#TextArea1").val(num);
alert("超过字数限制,多出的字将被截断!" );
}
else
{
$("#textCount").text(5-$("#TextArea1").val().length);
}
}
</script>
</head>
<body>
剩余<span id="textCount">5</span>个字<br />
<textarea name="textarea" id="TextArea1" cols="45" rows="5" onkeyup="words_deal();" ></textarea>
</body>
</html>

代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery限制输入字数,并提示剩余字数</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function words_deal()
{
var curLength=$("#TextArea1").val().length;
if(curLength>5)
{
var num=$("#TextArea1").val().substr(0,5);
$("#TextArea1").val(num);
alert("超过字数限制,多出的字将被截断!" );
}
else
{
$("#textCount").text(5-$("#TextArea1").val().length);
}
}
</script>
</head>
<body>
剩余<span id="textCount">5</span>个字<br />
<textarea name="textarea" id="TextArea1" cols="45" rows="5" onkeyup="words_deal();" ></textarea>
</body>
</html>

觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!

本文固定链接: http://www.js-code.com/js/js_1350.html