如果用户输入的字符数多于textarea
允许的字符数,我该如何截断它们?
相关代码:
<script type="text/javascript">
$(document).ready(function(){
//default usage
$("#message1").charCount();
//custom usage
$("#message2").charCount({
allowed: 50,
warning: 20,
counterText: 'Characters left: '
});
});
</script>
<label for="message">Type your message</label>
<textarea id="message1" name="message1"></textarea>
答案 0 :(得分:0)
$("#message1").change(function(){
var length = 50;
var text = $(this).text()
if(text.length > length){
var myTruncatedString = text.substring(0,length);
$(this).text(myTruncatedString);
}
});