如何将短消息限制在 300 字以内,并在消息框顶部显示字数?当我尝试输入内容时,消息框顶部的数字似乎并没有减少。
Javascript:
<script type="text/javascript" language="javascript">
var content;
$('textarea').on('keyup', function(){
var words = $(this).val().split(" ").length;
$('#myWordCount').text("("+(300-words)+" words left)");
if(words>=300){
$(this).val(content);
alert('no more than 300 words, please!');
} else {
content = $(this).val();
}
});
</script>
留言表格:
<form action="read_message.php" method="post">
<table class="form_table">
<tr>
<td style="font-weight:bold;">Subject:</td>
<td><input style=" width:300px" name="form_subject"/></td>
<td></td>
</tr>
<tr>
<td style="font-weight:bold;">Message:</td>
<td id="myWordCount">300 words left</td>
<td></td>
</tr>
<tr>
<td><input type="hidden" name="sender_id" value="<?php echo $sender_id?>"></td>
<td><textarea cols="50" rows="4" name="form_message"></textarea></td>
<td valign="bottom"><input type="submit" name="submit_message" value="send"></td>
</tr>
</table>
</form>