基本的な考え方は、入力で指定された長さの値の後の文字を強調表示し、通知メッセージも表示することです。
どうぞ:
<div id="test_box">
<input type="text" id="text_text">
</div>
CSS:
#notice {
width: 140px;
height: 40px;
background-color: black;
}
#test_box {
width: 400px;
height: 400px;
}
および jQuery コード:
$(document).ready(function() {
var length = $('#text_text').val().length;
var char_limit = 5;
$('#text_text').bind('keyup', function() {
new_length = $('#text_text').val().length;
if (new_length > char_limit) {
$('#text_text').css('color','red');
$('#test_box').append('<div id="notice"> There are limit of character for home page title of this field </div>'); // wrong too much divs :/
} else {
$('#text_text').css('color', 'black');
$('#notice').hide(); //wrong
}
});
});
後に強調表示された文字が超えた瞬間char_limit
に、私が必要とするのは、後に続く人だけを強調表示することchar_limit
です。また、文字を入力すると毎回ブロックが追加されることに注意してください。そのdivを手動で作成するか、作成しないで、それchar_limit
を超えたときに何らかの形で表示する必要があると思います。