Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript textarea最近字段的计数字符_Javascript_Html_Jquery - Fatal编程技术网

Javascript textarea最近字段的计数字符

Javascript textarea最近字段的计数字符,javascript,html,jquery,Javascript,Html,Jquery,我的页面上有4-5个文本区域,我已经设置了字符的限制。用户可以添加500个字符 现在我的问题是,我必须显示最近字段的字符数。到目前为止,它正在向所有人展示。我的意思是,如果我在第一个textarea中添加任何内容,然后计算所有textarea的显示 请检查下图 我在尝试这样的事情 current = $(this).closest('.row .valInfo').find('.currentchar'), maximum = $(this).closest('.valInfo').f

我的页面上有4-5个文本区域,我已经设置了字符的限制。用户可以添加500个字符

现在我的问题是,我必须显示最近字段的字符数。到目前为止,它正在向所有人展示。我的意思是,如果我在第一个textarea中添加任何内容,然后计算所有textarea的显示

请检查下图

我在尝试这样的事情

  current = $(this).closest('.row .valInfo').find('.currentchar'),
  maximum = $(this).closest('.valInfo').find('.maximumchar'),
  //theCount = $('.valInfo');
  theCount = $(this).closest('.valInfo')
$('.characterCount').keyup(函数(){
var characterCount=$(this).val().length,
当前=$('.currentchar'),
最大值=$('.maximumchar'),
计数=$('.valInfo');
当前.文本(字符计数);
}
);

0/500个字符
0/500个字符

您希望找到与已编辑元素相关的
currentchar
maximumchar
valinfo
元素。例如,通过仅在共享父级中查找匹配元素:

$('.characterCount').keyup(函数(){
var elem=$(此),
characterCount=elem.val().length;
current=elem.parent().find('.currentchar'),
maximum=elem.parent().find('.maximumchar'),
count=elem.parent().find('.valInfo');
当前.文本(字符计数);
}
);

0/500个字符
0/500个字符

是的,它正在按预期工作。谢谢你的快速回复。从我的大小向上投票。你可以用数据属性和伪元素后的css来做。这将是更多shorter@MisterJojo,你能和我分享这个例子吗?好的,让我说几句对不起,我错了,textarea元素不能使用伪元素。但是可以使用可编辑的内容对div执行此操作attribute@MisterJojo,没有问题,我认为,在这个场景中,脚本是好的。