当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jquery限制输入字数,并提示剩余字数实现代码

jquery限制输入字数,并提示剩余字数实现代码

2018年09月14日  | 移动技术网IT编程  | 我要评论

代码如下:

<!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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网