我有一个jquery代码,我想显示当前输入的字符数,剩余字符以及例如SMS的数量。 0到160之间的值是1个SMS,任何高于该值且低于321的值是两个SMS。 160以上的值打印2 sms到文档但初始值永远不会变为1.它始终为零。我怎么做到这一点?这是jquery代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var text_max = 481;
$('#textarea_feedback').html(text_max + ' characters remaining');
$('#textarea1').keyup(function() {
var text_length = $('#textarea1').val().length;
var cur = text_length;
var text_remaining = text_max - text_length;
$('#textarea_feedback').html(text_remaining + ' characters remaining');
$('#textarea_cur').html(cur + ' current characters');
var sms = 1;
$('#smsNum').html(sms + ' SMS');
if(cur <= 160) {
$('#smsNum').html(sms);
}if(cur >= 161) {
$('#smsNum').html(sms += 1 );
}else{
$('#smsNum').html(sms -= 1 );
}
});
});
</script>
The HTML code:
<span id="textarea_cur"></span>
<div id="smsNum"> SMS </div>
<textarea id="textarea1" rows="5" cols="40" maxlength="482" >
</textarea>
<span id="textarea_feedback">
</span>
答案 0 :(得分:1)
if(cur >= 161) {
$('#smsNum').html(sms += 1 );
}
这只会给你2个短信,无论你在160后增加多少字符数,因为你没有计算短信的数量,只需要你的sms = cur/160
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var text_max = 481;
$('#textarea_feedback').html(text_max + ' characters remaining');
$('#textarea1').keyup(function() {
var text_length = $('#textarea1').val().length;
var cur = text_length;
var text_remaining = text_max - text_length;
$('#textarea_feedback').html(text_remaining + ' characters remaining');
$('#textarea_cur').html(cur + ' current characters');
var sms = parseInt(cur/160);
$('#smsNum').html(sms + ' SMS');
});
});
</script>
The HTML code:
<span id="textarea_cur"></span>
<div id="smsNum"> SMS </div>
<textarea id="textarea1" rows="5" cols="40" maxlength="482" >
</textarea>
<span id="textarea_feedback">
</span>
答案 1 :(得分:1)
您可以160
使用除法的Math.ceil
。
sms = Math.ceil(cur / 160)
$(document).ready(function() {
var text_max = 481;
$('#textarea_feedback').html(text_max + ' characters remaining');
$('#textarea1').keyup(function() {
var text_length = $('#textarea1').val().length,
cur = text_length,
sms = Math.ceil(cur / 160),
text_remaining = text_max - text_length;
$('#textarea_feedback').html(text_remaining + ' characters remaining');
$('#textarea_cur').html(cur + ' current characters');
$('#smsNum').html(sms + ' SMS');
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="textarea_cur"></span>
<div id="smsNum"> SMS </div>
<textarea id="textarea1" rows="5" cols="40" maxlength="482"></textarea>
<span id="textarea_feedback"></span>
&#13;
答案 2 :(得分:0)
$(document).ready(function() {
var text_max = 481;
$('#textarea_feedback').html(text_max + ' characters remaining');
$('#textarea1').on('input',function() {
var text_length = $('#textarea1').val().length;
var cur = text_length;
var text_remaining = text_max - text_length;
$('#textarea_feedback').html(text_remaining + ' characters remaining');
$('#textarea_cur').html(cur + ' current characters');
var sms = 1;
$('#smsNum').html(sms + ' SMS');
if(cur <= 10) {
$('#smsNum').html(sms + ' SMS');
}
if(cur >= 11) {
$('#smsNum').html(sms += 1 );
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
</script>
The HTML code:
<span id="textarea_cur"></span>
<div id="smsNum"> SMS </div>
<textarea id="textarea1" rows="5" cols="40" maxlength="482" >
</textarea>
<span id="textarea_feedback">
</span>
尝试这个工作
答案 3 :(得分:0)
$(document).ready(function() {
var text_max = 481;
$('#textarea1').val('');
$('#textarea_feedback').html(text_max + ' characters remaining');
var sms = 0;
$('#smsNum').html(sms + ' SMS');
$('#textarea1').keyup(function() {
var text_length = $('#textarea1').val().length;
var text_remaining = text_max - text_length;
if ( text_length > 0 ) {
if ( text_length > 160 ) {
sms = 2;
} else {
sms = 1;
}
} else {
sms = 0;
}
$('#textarea_feedback').html(text_remaining + ' characters remaining');
$('#textarea_cur').html(text_length + ' current characters');
$('#smsNum').html(sms);
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The HTML code:
<span id="textarea_cur"></span>
<div id="smsNum"> SMS </div>
<textarea id="textarea1" rows="5" cols="40" maxlength="482">
</textarea>
<span id="textarea_feedback">
</span>
&#13;