按照代码计算用户仍在编写消息的消息数量
<script>
if (($('#Msg').val().length) <= 70) {
$('#smscount').text(Math.ceil(($('#Msg').val().length) / 70));
}
else
{
$('#smscount').text(Math.ceil(($('#Msg').val().length) / 67));
}
</script>
但它一直除以70,当消息长度超过70个字符时,我需要将除法器更改为67,我的意思是在我的情况下无法访问else语句中的代码,为什么?
最佳答案
假设您想要处理文本框的 keyup
事件,以便在您键入时更新值,那么脚本应该是
var count = $('#smscount'); // cache it
$('#Msg').keyup(function() {
var l = $(this).val().length;
if(l <= 70) {
count.text(Math.ceil(l / 70));
} else {
count.text(Math.ceil(l / 67));
}
});
或者,您可以只处理 .change()
或 .blur()
事件,以在文本框失去焦点时更新值。
关于javascript - js中如何动态计算字符串长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29643363/