<!doctype html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <input type="text" name="" id="" maxlength="6" οnkeyup="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>