js限制输入框字数

复制代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>

<body>


<input type="text" name="" id="" maxlength="6" onkeyup="checkInputCount(this, 6);" /><span>0/6</span>

<script type="text/javascript">
function checkInputCount(elem, count){
    elem.value = elem.value.substring(0, elem.maxlength);
    elem.nextSibling.innerHTML = elem.value.length + '/' + count;
}
</script>

</body>
</html>
复制代码

 

 

复制代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>

<body>

<input type="text" maxlength="6" xid="textContent" ><span><em>0</em>/6</span>

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(function(){
    $('[xid="textContent"]').keyup(function(){
        $(this).next('span').children('em').html($(this).val().length);
    });
/*    $('[xid="textContent"]').blur(function(){
        alert($(this).val())
    });*/
});


</script>

</body>
</html>
复制代码

原文地址:

http://write.blog.csdn.net/postedit

 

你可能感兴趣的:(html,js,html5,Web应用,HTML5技术)