gpt4 book ai didi

javascript - 文本区域字符计数器

转载 作者:行者123 更新时间:2023-12-01 02:28:45 24 4
gpt4 key购买 nike

window.onload = function() {                                                                                        
var text_max = 200;
$('#count_message').html('0 / ' + text_max );

$('#text').keyup (function() {
var text_length = $('#text').val().length;
var text_remaining = text_max - text_length;

$('#count_message').html(text_length + ' / ' + text_max);
});}

我有一个计算文本区域中字符的脚本。但它是在文本区域中实现的,其中已经写了一些东西。

简单来说,我只是想知道如何让这个脚本在页面加载时检查字符,而不是在我按下某些东西之后。

我的意思是。当我重新加载页面时,还剩下 0/200 个字符,但该文本区域中已经有大约 150 个字符

最佳答案

选项 1:

在页面加载时触发 keyup 事件,如下所示:

$('#count_message').keyup();

选项 2:

创建一个 keyup 事件处理函数,并在 keyup 和页面加载上使用它,如下所示:

$(function() {
var text_max = 200;

function onKeyup() {
var text_length = $('#text').val().length;
var text_remaining = text_max - text_length;
$('#count_message').html(text_length + ' / ' + text_max);
}

onKeyup();
$('#count_message').keyup(onKeyup);
});

关于javascript - 文本区域字符计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48487780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com