所以我得到了这个代码,但我无法弄清楚如何让它在一个字幕之后停止,我怎么能这样做?提前感谢您的任何答案!
<!doctype html>
<html>
<head>
<title>Hover</title>
<script type="text/javascript"
src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-
1.8.1.min.js">
</script>
</head>
<body>
<h1>Keyup</h1>
<hr>
<form action="#">
<textarea rows="10" cols="20" id="message" name="message">
</textarea>
<p>Characters remaining: <span id="remaining">50</span></p>
</form>
<script type="text/javascript">
$(document).ready(function(){
var maxCharacters = 50;
$("#message").on("keyup", function(){
var currentVal = $("#message").val().length;
var totalRemaining = maxCharacters - currentVal;
$("#remaining").text(totalRemaining);
});
});
</script>
</body>
</html>
&#13;
答案 0 :(得分:1)
最快的解决方案(没有任何js编码),将设置maxlength。
<textarea maxlength="50" rows="10" cols="20" id="message" name="message"></textarea>
答案 1 :(得分:0)
这就是你输入黑色的方式:
$("#message").attr("disabled");
您需要将此代码添加到侦听器中,无论是在您的键盘功能中还是在
上答案 2 :(得分:0)
尝试DEMO
$(document).ready(function(){
var maxCharacters = 50;
$("#message").on("keyup keydown", function(event){
var currentVal = $("#message").val().length;
var totalRemaining = maxCharacters - currentVal;
if(+$(this).val().length >= maxCharacters)
event.preventDefault(); // stop the default action of the event
$("#remaining").text(totalRemaining);
});
});
希望它有所帮助...
答案 3 :(得分:0)
$(document).ready(function(){
var maxCharacters = 50;
$("#message").on("keyup", function(){
var currentVal = $("#message").val().slice(0, maxCharacters);
$("#message").val(currentVal);
var totalRemaining = maxCharacters - currentVal.length;
$("#remaining").text(totalRemaining);
});
});