jquery限制输入字数,并提示剩余字数实现代码
代码如下:
<!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>
上一篇: 顺丰丰修入驻国内最大分期电商分期乐