清除文本区域并重置文本区域的字符数

编程入门 行业动态 更新时间:2024-10-28 02:36:31
本文介绍了清除文本区域并重置文本区域的字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个文本区域.在文本区域下方,我可以在用户键入时在文本区域中显示字符数.

I have a text area. Underneath the text area I can display the count of the characters in the text area as the user types.

我还有一个按钮供用户清除文本区域的内容.

I also have a button for the user to clear the contents of the text area.

我感到困惑的是,当用户单击清除按钮时,文本区域被清除,但字符数仍保持在以前的数上,而实际上该数应为零.

The issue I have and I am baffled by is that when the user clicks on the clear button, the text area is cleared, but the character count remains at the previous count, when actually the count should be zero.

我在绑定中使用了 keyup焦点模糊更改,但是当用户单击清除按钮时,字符数仍然没有变化.直到我用鼠标聚焦在文本区域时,计数的显示才会恢复为零.

I have used keyup focus blur change in the bind, but the character count still does not change when the user clicks on the clear button. The display of the count does not return to zero, until I focus in the text area with the mouse.

有什么建议吗?

这是我的HTML代码:

Here is my HTML code:

<textarea cols="40" id="id_objective_details" maxlength="1000" name="objective_details" rows="10">Test</textarea> <span id="id_char_count"></span><span> of 1,000 character limit</span> <i id="id_icon_reset" class="fa fa-ban blue_color icon_size20" rel="tooltip" html="true" data-placement="top" onclick="resetCharacterCount();focusTextArea();" title="Clear"></i>

这是JS/JQ代码:

$(document).ready(function() { // bind displaying the character count of the text area to the keyup event (function on base.html). displayCharacterCount('id_objective_details', 'id_char_count'); } function displayCharacterCount(id1, id2) { $('#' + id1).bind("keyup focus blur change", function () { numeral.language('{{ LANGUAGE_CODE }}'); var charCount = numeral($(this).val().length).format('0,0'); $("#" + id2).text(charCount); }); } function resetCharacterCount() { displayCharacterCount('id_objective_details', 'id_char_count'); } function focusTextArea() { $('#id_objective_details').focus(); }

推荐答案

我尝试了一下,它就可以了!

I try this and it's work!

<textarea cols="40" id="id_objective_details" maxlength="1000" name="objective_details" rows="10">Test</textarea> <span id="id_char_count"></span><span> of 1,000 character limit</span> <i id="id_icon_reset" class="fa fa-ban blue_color icon_size20" rel="tooltip" html="true" data-placement="top" onclick="resetCharacterCount();focusTextArea();" title="Clear">clear</i> <script src="ajax.googleapis/ajax/libs/jquery/1.12.2/jquery.min.js"> </script> <script> $(document).ready(function() { $('#id_objective_details').keyup(function(){ $('#id_char_count').text($('#id_objective_details').val().length); }); }); function resetCharacterCount() { $('#id_char_count').text(0); $('#id_objective_details').val(''); } function focusTextArea() { $('#id_objective_details').focus(); } </script>

更多推荐

清除文本区域并重置文本区域的字符数

本文发布于:2023-11-10 00:27:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573855.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   区域   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!